我正在尝试创建一个 UITableViewCell 的子类,它具有我的应用程序用于其多个 TableView 中每个单元格的自定义样式。我正在为 iOS 7.1 构建它。由于某种原因,在 initWithStyle
中设置样式似乎没有效果。
我的代码:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
[self.textLabel setTextColor:[UIColor redColor]];
[self.contentView setBackgroundColor:[UIColor greenColor]];
}
return self;
}
在我的 Storyboard中,我将 UITableViewController
中的单元格类设置为 MYSubTableViewCell
。我将表格 View 单元格样式设置为基本,我也将其设置为自定义,但没有效果。
在 MyTableViewController.m
中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MYSubTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
return cell;
}
我需要具有预定义背景和文本的单元格,这是正确的方法吗?
最佳答案
问题是,当您的单元格从 Storyboard实例化时,单元格未使用 -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
进行初始化。它是用 - (id)initWithCoder:(NSCoder *)aDecoder
初始化的。
你的初始化器永远不会被调用。您应该改写 initWithCoder:
。
关于ios - 子类化 UITableViewCell 不显示添加的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25111431/