简体   繁体   中英

Sublassing UITableViewCell and using it in a Storyboard

I am testing a customized UITableViewCell where the pieces are customized in initWithStyle and the controls are created in code. I am testing to see whether what we can use this cell with storyboards and prototype cells in a UITableView and use the interactions provided by storyboard. The cell that I'm using is called EvenCell.

If I remove the prototype cell and instantiate the cell in code like this:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    EvenCell *cell=[tableView dequeueReusableCellWithIdentifier:@"MyCell"];

    if(cell==nil)
        cell=[[EvenCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"];

    cell.cellTitleLabel.text=[allKeys objectAtIndex:indexPath.row];
    return cell;
}

it works.

在此处输入图片说明

But when I set up a prototype cell and set it's class to EvenCell and reuse identifier to "MyCell", it doesn't work. What am I doing wrong or is what I'm trying to do possible?

在此处输入图片说明

在此处输入图片说明

If I see correctly, you have empty cell in second example. You need to add subviews to your cell content in interface builder and connect it as outlets with your EvenCell class. For example: Add Label to your custom cell in interface builder, than make an outlet in EvenCell class for that label, you can name outlet for ex. titleLabel and than in your tableview:cellForRowAtIndexPath set your label text property by cell.titleLabel.text = [allKeys objectAtIndex:indexPath.row]

I've just spotted that I missed part about adding new views to cell content in initWithStyle method. So I've just created example code in xCode and it worked for me even if cell subviews are added programatically in EvenCell initWithStyle method. This is example:

#import <UIKit/UIKit.h>
@interface EvenCell : UITableViewCell
@property (nonatomic, strong) UILabel *textLabel;
@end

#import "EvenCell.h"

@implementation EvenCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {

    self.textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 50, 20)];
    [self.contentView addSubview:self.textLabel];

}
return self;}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

@end

than In your tableView:cellForRowAtIndexPath: method

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"MyCell";

EvenCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];

if(cell == nil) {
    cell = [[testCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}

cell.textLabel.text = @"Example title";

return cell;
}

And in interface builder set dynamic prototypes cells for UITableView, set cell class to EvenCell and set cell identifier to MyCell and it has to work. I've just check it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM