2013-02-11 47 views
0

对不起,我错过了它。我有一个美丽的桌子,正确的(可变)行高。但所有的细胞都是空白的。应在每个单元格中填充三个标签。虽然来自heightForRowAtIndexPath的单元格正确,但自定义UITableViewCell为空

UPDATE:FIX低于

@implementation MasterTableCell 

@synthesize labelDesc; 
@synthesize labelDuration; 
@synthesize labelName; 

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


    // name 
    CGRect frameTextLabel = CGRectMake(8, 2, 244, 25); 
    labelName = [[UILabel alloc] initWithFrame:frameTextLabel]; 
    labelName.lineBreakMode = UILineBreakModeTailTruncation; 
    labelName.font = [UIFont boldSystemFontOfSize:17.0]; 
    labelName.textColor = [UIColor blackColor]; 

    // description 
    labelDesc = [[UILabel alloc] init]; 
    labelDesc.lineBreakMode = UILineBreakModeWordWrap; 
    labelDesc.numberOfLines = 0; 
    labelDesc.textColor = [UIColor grayColor]; 
    labelDesc.font = [UIFont systemFontOfSize:14.0f]; 
    labelDesc.backgroundColor = [UIColor blueColor]; 

    // duration 
    CGRect frame = CGRectMake(252, 5, 40, 20); 
    labelDuration = [[UILabel alloc] initWithFrame:frame]; 
    labelDuration.font = [UIFont boldSystemFontOfSize:14.f ]; 
    labelDuration.textAlignment = UITextAlignmentRight; 
    labelDuration.backgroundColor = [UIColor redColor]; // to see it 

    [self.contentView addSubview:labelName]; 
    [self.contentView addSubview:labelDesc]; 
    [self.contentView addSubview:labelDuration]; 

    } 
    return self; 
} 

@end 

而且

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

    MasterTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
    cell = [[MasterTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    Recipe *recipeAtIndex = [sortedArray objectAtIndex:indexPath.row]; 

    cell.labelName.text = @"Test1"; 
    cell.labelDesc.text = @"Test2"; 
    cell.labelDuration.text = [TBCommon formattedStringforDuration:recipeAtIndex.duration withDelimeter:@":"]; 

    CGRect frameDescLabel = CGRectMake(8, 25, 284, [self heightForDescriptionFrame:recipeAtIndex.description]); 
    cell.labelDesc.frame = frameDescLabel; 

    return cell; 
} 

FIX

#import "MasterTableCell.h" 

@implementation MasterTableCell : UITableViewCell 

@synthesize labelDesc; 
@synthesize labelDuration; 
@synthesize labelName; 

- (void)awakeFromNib 
{ 
    [super awakeFromNib]; 

// name 
CGRect frameTextLabel = CGRectMake(8, 2, 244, 25); 
labelName = [[UILabel alloc] initWithFrame:frameTextLabel]; 
labelName.lineBreakMode = UILineBreakModeTailTruncation; 
labelName.font = [UIFont boldSystemFontOfSize:17.0]; 
labelName.textColor = [UIColor blackColor]; 

// description 
labelDesc = [[UILabel alloc] init]; 
labelDesc.lineBreakMode = UILineBreakModeWordWrap; 
labelDesc.numberOfLines = 0; 
labelDesc.textColor = [UIColor grayColor]; 
labelDesc.font = [UIFont systemFontOfSize:14.0f]; 

// duration 
CGRect frame = CGRectMake(252, 5, 40, 20); 
labelDuration = [[UILabel alloc] initWithFrame:frame]; 
labelDuration.textColor = [UIColor colorWithRed:38.0/255.0 green:111.0/255.0 blue:208.0/255.0 alpha:1]; 
labelDuration.font = [UIFont boldSystemFontOfSize:14.f ]; 
labelDuration.textAlignment = UITextAlignmentRight; 

[self.contentView addSubview:labelName]; 
[self.contentView addSubview:labelDesc]; 
[self.contentView addSubview:labelDuration]; 
} 

@end 

而且

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

static NSString *CellIdentifier = @"Cell"; 

MasterTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

Recipe *recipeAtIndex = [sortedArray objectAtIndex:indexPath.row]; 

cell.labelName.text = recipeAtIndex.name; 
cell.labelDesc.text = recipeAtIndex.description; 
cell.labelDuration.text = [TBCommon formattedStringforDuration:recipeAtIndex.duration withDelimeter:@":"]; 

CGRect frameDescLabel = CGRectMake(8, 25, 284, [self heightForDescriptionFrame:recipeAtIndex.description]); 
cell.labelDesc.frame = frameDescLabel; 

return cell; 

} 
+0

你是否检查过你的单元格标识符和它在xib或storyboard中的拼写? – 2013-02-11 21:11:18

+0

同时检查你的数组,并确保你正在提取的'Recipe'项目仍然有它的数据。 – GeneralMike 2013-02-11 21:16:42

+0

单元格标识符看起来不错,我正在使用Storyboards,并且我将Custom Class的类设置为'MasterTableCell'。我用静态字符串替换了'Recipe'中的值,以确保这不是问题。 :( – ToddB 2013-02-11 21:24:13

回答

1

如果您使用的是Storyboard,initWithStyle将永远不会被调用。将标签创建代码移动到awakeFromNib中。

因为dequeueReusableCell总是返回一个单元格,所以您可以摆脱整个if(cell == nil)部分。

+0

我在故事板程序中使用initWithStyle,它似乎对我来说工作得很好。 – GeneralMike 2013-02-11 21:39:55

+0

就是这样!我从来没有想过将创建代码放在awakeFromNib中。哇。我将使用正在工作的新版本进行更新。我不明白为什么这个工作,需要挖掘它。 – ToddB 2013-02-11 21:50:18

+0

从nib加载的任何对象在休眠状态下都会调用-awakeFromNib方法。这是苹果不透明的一部分,我认为它是有害的 - 他们没有明确地说任何地方(我知道)故事板是用笔尖实现的,尽管这是一个明显的结论。 – jsd 2013-02-12 03:30:41

0

您是否认为您的表视图委托/数据源方法实际上被调用。尝试在cellForRowAtIndexPath方法中放置一个断点来验证。如果断点没有被打开,这意味着你的委托和数据源没有设置。设置这些。

我注意到的另一件事是,你没有为你的labelDesc实例变量设置框架。值得一看。

+0

我也注意到框架的东西,但是他将它设置在他的'cellForRowAtIndexPath'中如果我的'NSLog'正在被使用(VC是'UITableViewController'的子类),我不认为这是造成问题。 – GeneralMike 2013-02-11 21:25:20

+0

正在使用委托和数据源(VC是'UITableViewController'的子类) “recipeAtIndex.duration”(例如)的值都很好。我在'cellForRowAtIndexPath'中为'labelDesc'设置了框架,因为这个标签的高度取决于它的内容(也许这是错误的方法?)。这是行得通的,行高是正确的,内容就是没有画。我将其中一个标签更改为红色背景,但没有看到它。 – ToddB 2013-02-11 21:32:15

+0

你有没有在NIB文件中的单元格?如果是,那么你必须像这样加载它: NSArray * items = [[NSBundle mainBundle] loadNibNamed:NibName owner:self options:nil]; cell = [items objectAtIndex:0]; – puru020 2013-02-11 21:37:36

0

当我设置我的自定义单元格子类时,我使用的教程让我使用名为layoutSubviews的单独方法设置框架。对你来说,它看起来像

- (void) layoutSubviews 
{ 
    [super layoutSubviews]; 

    labelName.frame = CGRectMake(8, 2, 244, 25); 
    labelDuration.frame = CGRectMake(252, 5, 40, 20); 
} 

当我有什么要补充的是动态位于像labelDesc是,我不得不完全拉出来我定制单元类,并把它所有cellForRowAtIndexPath细胞(所以我会有labelDesc = [[UILabel alloc] initWithFrame...cellForRowAtIndexPath之类的东西,在initWithStyle中没有任何东西)。

我不知道为什么这样做是与在initWithStyle中设置框架有什么不同,但它似乎为我工作。