美文网首页
点击cell时背景颜色的改变

点击cell时背景颜色的改变

作者: 小小的叶子随枫飘落 | 来源:发表于2016-09-29 15:18 被阅读217次

    1、系统默认的背景颜色

    //无色

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    //蓝色     UITableViewCellSelectionStyleBlue;

    //灰色     UITableViewCellSelectionStyleGray;

    2、自定义颜色和背景设置

    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];

    cell.selectedBackgroundView.backgroundColor = [UIColor redColor];

    当再次回到这个页面的时候cell还是点击的状态,但是添加这段代码的时候就解决了这个问题了,代码如下:

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    3、但是这样设置的时候,cell上的子控件要是设置背景颜色的时候,点击的瞬间和长按的时候颜色会被覆盖的,这个时候我们需要在自定义的cell中实现下面方法,保证点击的瞬间或者长按的时候颜色不会被覆盖,代码如下

    例如UILabel

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    _cellLabel.backgroundColor = [UIColor redColor];

    }

    - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{

    [super setHighlighted:highlighted animated:animated];

    _cellLabel.backgroundColor = [UIColor redColor];

    }

    一个是选择状态 一个是高亮的状态

    OK,这个问题就完美的解决了!

    相关文章

      网友评论

          本文标题:点击cell时背景颜色的改变

          本文链接:https://www.haomeiwen.com/subject/qvakyttx.html