UITableViewCell在编辑状态下如何修改背景颜色

发布时间:2021-07-23 09:24:34 作者:小新
来源:亿速云 阅读:133

小编给大家分享一下UITableViewCell在编辑状态下如何修改背景颜色,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

一、先看下效果图

UITableViewCell在编辑状态下如何修改背景颜色

二、网上很多下面这种答案

UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;

这样设置,蓝色的选中图标也不会出现.

这种仅限于不编辑的时候,让TableViewCell没有灰色高亮.

三、具体实现:

(1).在创建cell的时候设置selectedBackgroundView

RealTimeControlTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

if (cell == nil) {
 cell = [[RealTimeControlTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
 cell.contentView.backgroundColor = [UIColor clearColor];
 UIView *backGroundView = [[UIView alloc]init];
 backGroundView.backgroundColor = [UIColor clearColor];
 cell.selectedBackgroundView = backGroundView;
}

(2).自定义一个UITableVIewCell重写

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
if (!self.editing) {
 return;
}
[super setSelected:selected animated:animated];

if (self.editing) {
 self.contentView.backgroundColor = [UIColor clearColor];
 self.textLabel.backgroundColor = [UIColor clearColor];
 self.detailTextLabel.backgroundColor = [UIColor clearColor];
}
}

(3)还要重写下面方法 因为在长按cell的时候也会高亮,出现灰色的背景

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
 return;
}

对上面第二步代码说明:

1.在非编辑状态下,默认不会出现选中效果,直接return.

return 以后还是会继续调用

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 在这里处理cell的点击事件
}

2.要实现选中的蓝色图标出现,以及添加cell到选中cell的数组.

调用系统的默认方法

[super setSelected:selected animated:animated];

3.在编辑状态下修改cell的contenView为clear,清除选中时候的灰色背景.

看完了这篇文章,相信你对“UITableViewCell在编辑状态下如何修改背景颜色”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. IOS UITabBarViewController 修改背景颜色
  2. UITableViewCell的编辑,移动,添加或者删除

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios uitableviewcell

上一篇:for,while,foreach遍历时间的比较

下一篇:js下载文件并修改文件名的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》