tableView的多选和单选

系统自带的多选

tableView的多选和单选_第1张图片
Snip20170605_1.png
  • 首先让tableView允许多选操作
 // 允许在编辑模式进行多选操作
 self.tableView.allowsMultipleSelectionDuringEditing = YES;
  • 当点击编辑的时候设置tableView的编辑模式
[self.tableView setEditing:YES animated:YES];
  • 如果你想让一个按钮来控制tableView的编辑和不编辑的状态可以这么写
[self.tableView setEditing:!self.tableView.isEditing animated:YES];
  • 完成以上几步就可以选择想要删除的cell了
  • 但是如果你想删除选中的数据要实现下面的方法
- (void)remove
{
    // 获得所有被选中的行
    NSArray *indexPaths = [self.tableView indexPathsForSelectedRows];

    
    // 便利所有的行号
    NSMutableArray *deletedDeals = [NSMutableArray array];
    for (NSIndexPath *path in indexPaths) {
        [deletedDeals addObject:self.deals[path.row]];
    }
    
    // 删除模型数据
    [self.deals removeObjectsInArray:deletedDeals];
    
   // 刷新表格  一定要刷新数据
    [self.tableView reloadData];
}
# 说明:self.deals 是存放模型的数组

自定义cell多选

tableView的多选和单选_第2张图片
Snip20170605_2.png
  • 首先开发模式是MVC思想(如果不是MVC思想往后看)
    • 给模型增加一个属性
    • 这个属性是用来显示或者隐藏 打钩 的图片的
/** 状态量标识有无被打钩 */
@property (assign, nonatomic, getter=isChecked) BOOL checked;
  • 然后再给cell赋值的时候判断cell子控件打钩图片的显示隐藏
// 设置打钩控件的显示和隐藏
self.checkView.hidden = !deal.isChecked;
  • 最后一步就是 给模型的checked属性赋值
    • 在tableView的点击方法中实现下面代码
    • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
// 取消选中这一行
[tableView deselectRowAtIndexPath:indexPath animated:YES];

// 模型的打钩属性取反
Deal *deal = self.deals[indexPath.row];
deal.checked = !deal.isChecked;

// 刷新表格
[tableView reloadData];

# 说明:Deal 是模型数据    self.deals是存放模型数据的数组

自定义cell多选(不是MVC开发模式)

  • 首先声明一个可变数组用来存放点击cell的indexPath
@property (nonatomic, strong) NSMutableArray *indexPaths;

# 并且进行懒加载
- (NSMutableArray *)indexPaths
{
    if (!_indexPaths) {
        _indexPaths = [NSMutableArray array];
    }
    return _indexPaths;
}

  • 然后再给你cell赋值的地方写下面代码
//mr_tb 未选中图片  xz_tb选中图片
#默认是没有任何选中的cell的
cell.imageView.image = [UIImage imageNamed:@"mr_tb"];
//多选
for (NSIndexPath * index in self.indexPaths) 
{//遍历数组里面的索引 和 当前索引是否一致
     if (index == indexPath)
     {//改变在选择的数组里面的记录
          cell.imageView.image = [UIImage imageNamed:@"xz_tb"];//选中
          break;
     }
}
  • 在tableView的点击代理方法中实现下面方法
//取出当前cell
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
UIImage * image = [UIImage imageNamed:@"xz_tb"];
if ([cell.imageView.image isEqual:image]) 
{//如果为选中 变成未选中
     cell.imageView.image = [UIImage imageNamed:@"mr_tb"];
     [self.indexPaths removeObject:indexPath];
}else{//如果为未选中 变成选中
      cell.imageView.image = [UIImage imageNamed:@"xz_tb"];
      [self.indexPaths addObject:indexPath];
}
  • 如果你想控制多选的个数的话 你可以这么写
 UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
UIImage * image = [UIImage imageNamed:@"xz_tb"];
if ([cell.imageView.image isEqual:image])
 {//如果为选中
       cell.imageView.image = [UIImage imageNamed:@"mr_tb"];
       [self.indexPaths removeObject:indexPath];
}else{
       if (self.indexPaths.count >= 2)
       {//如果当前数组存储的索引超过两个直接返回
        [self showMessage:@"最多只能选择两个"];
         return;
        }else{
                cell.imageView.image = [UIImage imageNamed:@"xz_tb"];
                [self.indexPaths addObject:indexPath];
            }
            
        }
  • 整体的思想就是当你选中某一个cell的时候取出当前cell的图片 和 选中图片进行对比 如果一样 就把当前cell的iamge变成另外一个图片 (当变成未选中的时候 要把在数组里面的indexPath移除,当变成选中的时候设置选中的图片 并且把当前选中的indexPath保存在数组中)

自定义cell单选

  • 在tableView的点击dialing方法中写下下面代码
for (int i = 0; i < self.deals.count; i++) {
        Deal *deal = self.deals[i];
        if (i != indexPath.row) {
            deal.checked = YES;
        }else{
            deal.checked = NO;
        }
    }
# Deal 是数据模型   self.deals 是存放数据模型的数组

你可能感兴趣的:(tableView的多选和单选)