iOS - UITableViewCell 改变编辑状态图片

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

自带的效果图是这样的:




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



我们一步步的来:


  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:@"要替换的图片"];
      }
      }
    • 加了这代码后效果是这样的:



  2. 可以看到图是已经换了,但是点击的效果没有去掉,下一步去掉点击的效果,我们可以根据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;
      }
      • 效果如下:



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


后续:

  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];
         }
     }
  2. 如何处理选中和未选中的数据呢?用系统自带的方法就可以,首先定义一个可变的数组@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 />

    一起学习进步

文/栋飞
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值