2014-01-15 119 views
0

我正在使用故事板執行我的項目,並試圖實施自定義UITableViewCell使用故事板時自定義uitableviewcell

我願做我的自定義單元格如下:

#import "CustomCell.h" 

@implementation CustomCell 

@synthesize myLabel, myButton; 

- (instancetype)initWithCoder:(NSCoder *)decoder 
{ 
    if ((self = [super initWithCoder:decoder])) 
    { 
     //want to custom setup of properties placed in the cell 
     self.myButton.backgroundColor = [UIColor redColor];//this does NOT work 
     //and so forth... 
    } 
    return self; 
} 

但底色未設置。它只有當我設置在tableViewControllercellForRowAtIndexPath功能按鈕的背景顏色,這樣

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomCell"]; 
    MyObject *obj = self.myObjects[indexPath.row]; 
    cell.myButton.backgroundColor = [UIColor redColor];//This works! 
    cell.myLabel.text = obj.name; 
    return cell; 
} 

作品和我已經設置斷點和NSLog嘗試調試和initWithCodercellForRowAtIndexPath之前要求的每一個細胞?? 但是當我將它設置在自定義單元格中時,單元格中按鈕的背景顏色不顯示。

任何幫助?

+0

爲什麼不在故事板中設置backgroundColor? –

回答

3

嘗試使用awakeFromNib方法而不是initWithCoder:來進行單元格的任何初始定製。正如評論中提到的,對於控件的背景顏色等簡單的事情,您可以通過故事板在Xcode中執行此操作。

+0

這樣做的竅門 - 謝謝! – jacob

相關問題