如果我有一个自定义 UITableViewCell ,它不使用内置于单元格中的 textLabel ,而是自己绘图,我该如何更改外观选择时 contentView ,就像默认文本一样(通过设置 selectedTextColor:来自定义)?

如果我更改 tableView:willSelectRowAtIndexPath:,那么它只会在蓝色选择背景启动后更新,但不会在它动画时更新,就像我想要的那样。

有帮助吗?

解决方案

只是不要将UITableViewCell子类化并使用默认行为。 您可以完全自定义单元格而无需任何子类化。

阅读本文了解更多详情。

其他提示

在tableview cellForRowAtIndexPath方法中添加此代码,只需更改UITableViewCell选择样式的预期颜色。

   //-------------------------------------------------------------------------
   //background selected view 
   UIView *viwSelectedBackgroundView=[[UIView alloc]init];
   viwSelectedBackgroundView.backgroundColor=[UIColor colorWithRed:124.0/255.0 green:202.0/255.0 blue:227.0/255.0 alpha:1.0];
   cell.selectedBackgroundView=viwSelectedBackgroundView;
   //-------------------------------------------------------------------------

如果您已经为UITableViewCell创建了子类,那么您可以通过覆盖以下内容来自定义单元格的元素:

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    if(highlighted) {
        self.backgroundColor = [UIColor redColor];
    } else {
        self.backgroundColor = [UIColor clearColor];
    }

    [super setHighlighted:highlighted animated:animated];
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top