Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

ios - Connecting IBOutlets to UITableViewCell prototype

I am creating a UITable with custom UITableViewCell prototype. My cell contains UIImageViews, UILabels and UIButtons.

When I control and drag from my buttons to my class' interface it works just fine. However, it does not work with outlets.

When I create the IBOutlet in the .h file, I can only connect if I select the UITable not the cell and of course the result is a broken app.

Do you guys have any idea how to solve this? I do not want to use a custom class just for the cell. I really would like to stick with Storyboard and prototypes.

Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 enter image description here

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

enter image description here

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
    {
NSIndexPath *indexPathOfPressedCell = [self.tableView indexPathForCell:cell];
    NSLog(@"Pressed");
    }

This will look like a long method but it's quite useful and readable...

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

Also, return the CustomCell height by implementing

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

it may occur....


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...