【发布时间】:2014-10-15 10:29:40
【问题描述】:
我的简单自定义UITableViewCell 子类在选择时不显示其内容。当我点击其中一个单元格时,绿色视图消失,我只看到单元格的红色背景色。当我选择另一个单元格时,之前选择的单元格的内容会再次出现。有什么想法吗?
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
{
self.backgroundColor = [UIColor redColor];
UIView* someView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
someView.backgroundColor = [UIColor greenColor];
[self.contentView addSubview:someView];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
我在视图控制器的 `viewDidLoad' 中注册了它:
[self.tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"Identifier"];
这是我创建它们的方法:
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Identifier"];
if (cell == nil)
{
cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"Identifier"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
【问题讨论】:
标签: ios objective-c uitableview