2013-02-11 31 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中設置框架有什麼不同,但它似乎爲我工作。

相關問題