2011-10-04 69 views
0

我已经创建了如下的UITableViewCell子类。我所做的所有事情都是在单元格的内容视图中添加一个标签并设置其文本。无法继承UITableViewCell

#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; 
} 

现在我用这个自定义单元格在我的表视图这样:

- (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:cellIdentifier] autorelease]; 
    } 
    return cell; 
} 

但是标签不会在我的表视图显示。我错过了什么?

+0

内部使用NSlog语句,并检查控制是否转到'init'方法。 – Legolas

回答

5

initWithFrame:reuseIdentifier:已弃用,UITableViewCell的指定初始值设定项为initWithStyle:reuseIdentifier:。可能当您使用当前方法时,contentView未创建,因此您将标签添加到nil

更重要的是,你甚至不打电话给你重写的方法 - 你已经覆盖普通的旧initWithFrame:

+0

所有内容都已连线并调用了cellForRowAtIndexPath。它只是带有标签的自定义单元格不显示! – NSExplorer

+0

查看更新后的答案,我做了更多的研究 – jrturton

+0

解决了这个问题。谢谢 – NSExplorer