Connecting IBOutlets to UITableViewCell prototype

核能气质少年 提交于 2019-11-28 13:55:41

Using Labels with tag will get the job done but never a good practise ... the best way is to create a custom class of UITableViewCell.

ie, select

New File>Cocoa Touch >Objective C Class

and Create it as a subclass of UITableViewCell now you will get .h and .m files..

Next step is to create the view of the cell for this

select

New File> User Interface > Empty

and now create this with same name of your customcell class (lets say "CustomCell")

now you will have three files CustomCell.h,CustomCell.m,CustomCell.xib

now select the xib file and add UITableViewCell object on the xib and set its custom class as "CustomCell"

Look below pic

now after this you can drag any thing (UIImageView,UITextfield,UIButton) to the below view and give outlets onto the CustomClass and manage the actions using delegate methods..

if you have imageView outlet as titleImage ..then you can access the same by creating the cell object in CellForRowAtIndex (TableView delgate method) to set image.

cell.titleImage=[UIImage ImageNamed:@"goo.png"]; 

now one more thing i have to say is that you have to implement an init method also in CustomCell.m to load the nib>>

it will look like the below code.

    -(id)initWithDelegate:(id)parent reuseIdentifier:(NSString *)reuseIdentifier
    {

        if (self = [self initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier])
        {
            self=(CustomCell*)[[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil] lastObject];
        }

        self.backgroundColor = [UIColor clearColor];
        self.backgroundView = NULL;
        self.selectedBackgroundView =NULL;

//If you want any delegate methods and if cell have delegate protocol defined
self.delegate=parent;

//return cell
    return self;
    }

Now it is better to have delegates if you are using buttons on your cell

so that in the button action method you can call delegate method (pass cell object) and implement the delegate in your ViewController with TableView

here is the example

now you can use your cell for UITableView to populate...and dont for get to set reuseIdentifier Value in CustomCell.xib (same as you set CustomClass)

lets set it namely ,hmm what else "customCell"

so while populating the tableView use

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier=@"customCell";
    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell==nil)
         cell= [[CustomCell alloc] initWithDelegate:self reuseIdentifier:cellIdentifier];

//set cell properties
   cell.titleImage=[UIImage ImageNamed:@"title.png"];



    return cell;
}

also dont forget to add delegate methods

give

ViewController:UIViewController<CustomCellDelegate>

on your ViewController.h file of ViewController

and then implement its body in your ViewController.m (implementation file)

as

-(void)cellButtonPressed:(CustomCell*)cell
{
NSLog(@"Pressed");
}

it is better to give index property to your cell to handle your table select deselect methods

provide

@property int index;

in your CustomCell.h

and also

add

cell.index=indexPath.row;

in your tableView cellAtRow delegate.....

This will look like a long method but its quiet useful and readable...

-------NB----------:

If you have any alignment issue just return the CustomCell height by implementing

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{}

it may occur....

You have to create a custom UITableViewCell class for the cell and then only cntrl + dragging to create outlets make sense , other wise there is a even simpler way. Have tags like 99343 , 99345(to avoid overlaps) to your UIImageViews , UILabels etc and access them like

  UILabel *myLabelFromPrototypeCell = (UILabel*)[cell.contentView viewWithTag:99343];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!