iOS - UITableViewCell 改变编辑状态图片
来源:互联网 发布:汽车代驾软件 编辑:程序博客网 时间:2024/06/06 00:13
UITableViewCell 是编辑状态时 会出现多选按钮,最近项目有需求这里要改成自己的图片和去掉一下点击效果,总结一下:
自带的效果图是这样的:
我们需要的效果是换掉 蓝色的选中图片和点击的背景颜色 效果大概是这样:
我们一步步的来:
首先把蓝色的选中图片换成自己的:方法就是先遍历cell的contentview得到这个图片然后替换,在自定义的cell里面找到
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
方法 具体代码:- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
if (!self.editing) return;
[super setSelected:selected animated:animated];
if (self.isEditing && self.isSelected) {
}self.contentView.backgroundColor = [UIColor clearColor]; //这里自定义了cell 就改变自定义控件的颜色 self.textLabel.backgroundColor = [UIColor clearColor]; UIControl *control = [self.subviews lastObject]; UIImageView * imgView = [[control subviews] objectAtIndex:0]; imgView.image = [UIImage imageNamed:@"要替换的图片"];
}
加了这代码后效果是这样的:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
可以看到图是已经换了,但是点击的效果没有去掉,下一步去掉点击的效果,我们可以根据cell的
selectedBackgroundView
属性来改变,具体代码:- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
}self.contentView.backgroundColor = [UIColor clearColor]; UIView *backGroundView = [[UIView alloc]init]; backGroundView.backgroundColor = [UIColor clearColor]; self.selectedBackgroundView = backGroundView; //以下自定义控件
return self;
}效果如下:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
这样基本已经好了,但是有的时候狂点系统的蓝色图标有时也会出来,发现是高亮状态的问题,所有在cell里面还要实现这个方法:
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
// [super setHighlighted:highlighted animated:animated]; // if (self.isEditing && self.isHighlighted ) { // UIControl *control = [self.subviews lastObject]; // UIImageView * imgView = [[control subviews] objectAtIndex:0]; // imgView.image = [UIImage imageNamed:@"DC_agree_selected"]; // } return;
}
里面把高亮状态的图片换成自己的就可以,我试了直接return也可以.
后续:
要实现编辑前UITableView要先进入编辑状态:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == (UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert)) { [self.dataSoure removeObject:[self.dataSoure objectAtIndex:indexPath.row]]; //animation后面有好几种删除的方法 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } }
如何处理选中和未选中的数据呢?用系统自带的方法就可以,首先定义一个可变的数组
@property (nonatomic,strong) NSMutableArray* selectArray;
,然后在tableView协议方法里面找到didSelectRowAtIndexPath
和didDeselectRowAtIndexPath
方法,具体实现:#pragma mark - UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self.selectArray addObject:[self.dataSoure objectAtIndex:indexPath.row]]; } - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { [self.selectArray removeObject:[self.dataSoure objectAtIndex:indexPath.row]]; }<br />
一起学习进步
文/栋飞
0 0
- iOS - UITableViewCell 改变编辑状态图片
- 让编辑状态下的UITableViewCell响应点击事件时,不改变背景色
- 让编辑状态下的UITableViewCell响应点击事件时,不改变背景色
- iOS UITableViewCell添加按钮 点击按钮改变按钮状态之坑
- iOS UITableViewCell 改变系统选中按钮(UITableViewCellEditControl)的默认图片和位置
- iOS 提升交互之根据多个UITextField的编辑状态改变按钮状态
- 改变UITableViewCell删除状态时,改变按钮的背景颜色。
- iOS 之点击按钮改变状态的图片
- iOS开发 UITableViewCell的背景改变问题
- 修复ios7 uitableviewcell 编辑状态下 删除按钮被覆盖
- 用JS改变表单的编辑状态
- IOS 改变图片
- Android EditText 不可编辑到可编辑状态改变
- uitableviewcell编辑模式自定义左边的图片按钮
- 状态条改变 for ios
- ios检测网络状态改变
- iOS UITableViewCell (左滑删除按钮及编辑模式)
- iOS UILable自适应撑高即UITableViewCell的高度自适应改变
- 墨香带你学Launcher之(一)-概述
- Maven-war-plugin插件中去除classes
- 栈的应用之计算机的运算方式
- 深入理解JVM—JVM内存模型
- python whl文件查找网站
- iOS - UITableViewCell 改变编辑状态图片
- js的push方法
- 多态实现原理
- 添加开机第一帧、第二帧logo图片
- (iOS开发) 录屏功能的实现~包括暂停和继续功能
- Jenkins(三)安装插件
- 让代码更优雅之解构赋值、扩展操作符
- 蓝桥杯算法训练 删除数组零元素
- MATLAB作图方法与技巧(三)