2014-01-15 114 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

相关问题