IOS-UITableView点击cell后子控件背景颜色消失

UITableview作为最常见的控件之一,使用的花样越来越多,页面也越来越多样化,所以,系统提供的cell的样式已不能满足我们开发者的需求,此时就需要自定义控件,有的时候,针对这些自定义cell的某个控件(比如UILabel),会加上背景颜色的设置,但是在点击cell的时候,这个背景颜色会消失,原因是:在点击cell的时候这个控件的高亮状态未设置颜色!!!

长按cell后中间两条线的颜色会消失,变成透明色的,就不上图了.

此时有两种方法解决,第一种,在代理方法中控制UITableViewCell的点选状态,为UITableViewCellSelectionStyleNone。

代码:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

        [tableView deselectRowAtIndexPath:indexPath animated:YES];

        ZPOrderCell *cell = [tableView cellForRowAtIndexPath:indexPath];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;

}

但是这样cell就没有点选状态了,其实就相当于一次性消费的感觉.

.如果需要有UITableViewCell的点选状态,但是不希望在点选以后其中控件的颜色并不变成透明色,那么方法为:

//这个方法在用户按住Cell时被调用

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

        [super setHighlighted:highlighted animated:animated];

        self.preLineLab.backgroundColor = [UIColor zp_colorWithHex:0xeeeee];

        self.nextLineLab.backgroundColor = [UIColor zp_colorWithHex:0xeeeee];

}

//这个方法是cell被选中或取消时调用

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

       [super setSelected:selected animated:animated];

       self.preLineLab.backgroundColor = [UIColor zp_colorWithHex:0xeeeee];

       self.nextLineLab.backgroundColor = [UIColor zp_colorWithHex:0xeeeee];

}

上边的[UIColor zp_colorWithHex:0xeeeee]是楼主自定义的UIColor的分类方法,用来传入一个十六进制的数据生成相应的颜色.跟本文章内容无关.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容