开发者

Unable to subclass UITableViewCell

I've created a UITableViewCell subclass as below. All I've done is added a label to the cell's content view and set its text.

#import <UIKit/UIKit.h>
@interface CustomTableViewCell : UITableViewCell {
}
@end

#import "CustomTableViewCell.h" 
@implementation CustomTableViewCell

@synthesize horizontalTableView;

- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame]))
    {

        UIView *myContentView = self.contentView;

        UILabel *l = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
        l.text = @"Hi";

        [myContentView addSubview:l];

        [l release];
    }
    return self;
}

Now I use this custom cell in my table view this way:

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

    CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[[CustomTableViewCell alloc] initWithFrame:CGRectMake(0, 0, 50, 300) reuseIdentifier:cellI开发者_运维知识库dentifier] autorelease];
    }
     return cell;
}

However the labels do not show in my table view. What am I missing ?


initWithFrame:reuseIdentifier:is deprecated, the designated initializer for UITableViewCell is initWithStyle:reuseIdentifier:. Probably when you are using the current method, contentView is not created, so you are adding your label to nil.

What's more, you aren't even calling your overridden method - you have overridden plain old initWithFrame:.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜