美文网首页iOS 技巧iOSDev
iOS 中 UITableView 实现多选的一种优雅的方案

iOS 中 UITableView 实现多选的一种优雅的方案

作者: HenryCheng | 来源:发表于2021-02-05 15:37 被阅读0次

在我们平时开发中,经常会遇到有个列表可以多选,然后选中后回到上一页,并把选中的数据带过去,或者把选中的结果提交,如下图

tableview_selection.gif

经常我们的做法是在数据源中做操作,比如将一个 model 添加一个 isSelected 属性,选中就置为 YES,然后刷新,最后提交时候遍历数据源,找到 isSelected 为 YES 的再做处理,如下


...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MMTestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MMTestTableViewCell" forIndexPath:indexPath];
    MMModel *model = _dataArray[indexPath.row];
    cell.statusLabel.text = model.title;
    if (model.isSelected) {
        cell.statusLabel.textColor = [UIColor redColor];
    } else {
        cell.statusLabel.textColor = [UIColor blackColor];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MMModel *model = _dataArray[indexPath.row];
    model.isSelected = !model.isSelected;
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

现在有种比较优雅的方式,不用去改 model,直利用 UITableView 的 allowsMultipleSelection 属性,就可以简单的去操作

1. 开启允许多选

// 开启允许多选,默认为 NO
self.tableView.allowsMultipleSelection = YES;

2. 分别在下面三个方法中作相应操作

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MMTestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MMTestTableViewCell" forIndexPath:indexPath];
    cell.statusLabel.text = _dataArray[indexPath.row][@"title"];
    if ([[tableView indexPathsForSelectedRows] containsObject:indexPath]) {
        cell.statusLabel.textColor = [UIColor redColor];
    } else {
        cell.statusLabel.textColor = [UIColor blackColor];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MMTestTableViewCell *cell = (MMTestTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    cell.statusLabel.textColor = [UIColor redColor];
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    MMTestTableViewCell *cell = (MMTestTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    cell.statusLabel.textColor = [UIColor blackColor];
}

3. 提取所选数据

NSString *res = @"";
for (NSIndexPath *indexPath in self.tableView.indexPathsForSelectedRows) {
    res = [res stringByAppendingString:@" "];
    res = [res stringByAppendingString:_dataArray[indexPath.row][@"title"]];
}
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选中的title" message:res preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"done" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 
}];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];

最后就可以得图示效果,这种方法简单方便,并且对 model 无入侵,比较优雅!

更多文章欢迎访问 https://github.com/loveway/Knowledge

相关文章

  • iOS 中 UITableView 实现多选的一种优雅的方案

    在我们平时开发中,经常会遇到有个列表可以多选,然后选中后回到上一页,并把选中的数据带过去,或者把选中的结果提交,如...

  • TableView基础

    总结一些UITableView常见的问题 和 常用的方法iOS UITableView的多选UITableView...

  • iOS UITableView的多选

    好久没写博客了,写些平常用的到的东西吧。一些列表经常需要编辑多选的功能,而UITableview自带多选删除的功能...

  • 优雅的使用UITableView(Swift 中)

    优雅的使用UITableView(Swift 中) 优雅的使用UITableView(Swift 中)

  • UITableView-01基础

    在iOS中要实现表格数据展示,最常用到的就是UITableView 是iOS比较重要的控件 UITableView...

  • UITableView实现多选

    今天做了一个UITableView多选的项目, 开始写的很麻烦, 还用了字典存储状态, 后来好好查了一下有关于多选...

  • UITableView

    什么是UITableView - 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView - ...

  • iOS UITableView 中实现多种cell的方案

    直接上代码; 1. 创建一个类IDOCellModel,继承NSObject,里面的属性如下: @interfac...

  • UITableView实现全选

    UITableView实现全选以及取消全选在此之前我曾经写过UITableView如果实现多选,在简书中,有需要的...

  • iOS有关架构组件化的文章链接

    iOS应用架构谈 组件化方案 iOS 组件化方案探索 iOS移动端架构的那些事 如何优雅的实现界面跳转 之 统跳协...

网友评论

    本文标题:iOS 中 UITableView 实现多选的一种优雅的方案

    本文链接:https://www.haomeiwen.com/subject/xrfgtltx.html