搜索
您的当前位置:首页正文

iOS - UITableViewCell 改变编辑状态图片

来源:二三娱乐

UITableViewCell 是编辑状态时 会出现多选按钮,最近项目有需求这里要改成自己的图片和去掉一下点击效果,总结一下:

自带的效果图是这样的:

<br />


我们需要的效果是换掉 蓝色的选中图片和点击的背景颜色 效果大概是这样:

<br />

我们一步步的来:

<br />

  1. 首先把蓝色的选中图片换成自己的:方法就是先遍历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:@"要替换的图片"];
      }
      }
  • 加了这代码后效果是这样的:

<br />


  1. 可以看到图是已经换了,但是点击的效果没有去掉,下一步去掉点击的效果,我们可以根据cell的selectedBackgroundView属性来改变,具体代码:

<br />
- (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;
}

  • 效果如下:

<br />


  1. 这样基本已经好了,但是有的时候狂点系统的蓝色图标有时也会出来,发现是高亮状态的问题,所有在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也可以.

<br />

后续:

  1. 要实现编辑前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];
        }
    }
  1. 如何处理选中和未选中的数据呢?用系统自带的方法就可以,首先定义一个可变的数组@property (nonatomic,strong) NSMutableArray* selectArray;,然后在tableView协议方法里面找到didSelectRowAtIndexPathdidDeselectRowAtIndexPath方法,具体实现:
    #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 />

一起学习进步

文/栋飞
Top