【问题标题】:Custom UITableViewCell does not show content when selected自定义 UITableViewCell 选择时不显示内容
【发布时间】: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


【解决方案1】:

单元格的背景应该在backgroundViewselectedBackgroundView 属性中设置,而不是作为contentView 的子视图添加。只有内容(文本、图像等)应该在contentView 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    相关资源
    最近更新 更多