使用故事板时自定义uitableviewcell
问题描述:
我正在使用故事板执行我的项目,并试图实施自定义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;
}
但底色未设置。它只有当我设置在tableViewController
的cellForRowAtIndexPath
功能按钮的背景颜色,这样
- (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
尝试调试和initWithCoder
被cellForRowAtIndexPath
之前要求的每一个细胞?? 但是当我将它设置在自定义单元格中时,单元格中按钮的背景颜色不显示。
任何帮助?
答
尝试使用awakeFromNib
方法而不是initWithCoder:
来进行单元格的任何初始定制。正如评论中提到的,对于控件的背景颜色等简单的事情,您可以通过故事板在Xcode中执行此操作。
+0
这样做的窍门 - 谢谢! – jacob
为什么不在故事板中设置backgroundColor? –