首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITableView多选

UITableView多选
EN

Stack Overflow用户
提问于 2010-06-15 05:02:20
回答 11查看 63.2K关注 0票数 48

如何将UITableView添加到基于视图的应用程序中,其中用户将点击多个单元格,该单元格将变为选中状态,就像时钟应用程序名为"Repeat“(Clock>Alarms> + >Repeat)的”新闹钟“设置一样,以及如何获取数组中所有选定的单元格?

EN

回答 11

Stack Overflow用户

回答已采纳

发布于 2010-06-15 05:31:09

在您的-tableView:didSelectRowAtIndexPath:实现中,您将根据当前值设置表格视图单元格的accessoryType属性(因此,它可以通过多次点击来打开和关闭)。例如:

代码语言:javascript
复制
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];

    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    } else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
}

除了单元格自己的附件类型状态之外,您还可以维护一个选定状态数组,或者遍历表视图中的单元格,查询每个单元格的状态,以便读出选定的行。

票数 29
EN

Stack Overflow用户

发布于 2012-12-12 04:27:55

对于多个选择,在viewDidLoad()中添加以下行

代码语言:javascript
复制
tableView.allowsMultipleSelection = true

tableView(_:cellForRowAt:)中将每个cell出队(或初始化)后对其进行配置

代码语言:javascript
复制
let selectedIndexPaths = tableView.indexPathsForSelectedRows
let rowIsSelected = selectedIndexPaths != nil && selectedIndexPaths!.contains(indexPath)
cell.accessoryType = rowIsSelected ? .checkmark : .none
// cell.accessoryView.hidden = !rowIsSelected // if using a custom image

在选中/取消选中时更新每个单元格

代码语言:javascript
复制
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)!
    cell.accessoryType = .checkmark
    // cell.accessoryView.hidden = false // if using a custom image
}

override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)!
    cell.accessoryType = .none
    // cell.accessoryView.hidden = true  // if using a custom image
}

完成后,获取所有选定行的数组

代码语言:javascript
复制
let selectedRows = tableView.indexPathsForSelectedRows

并获取所选数据,其中dataArray映射到只有一个部分的表视图中的行

代码语言:javascript
复制
let selectedData = selectedRows?.map { dataArray[$0.row].ID }
票数 148
EN

Stack Overflow用户

发布于 2013-05-29 22:26:04

@BrendanBreg implementation对我不起作用。@RaphaelOliveira提供了good solution,但是当你向下滚动表格时-错误的行被选中(因为UITableView缓存了它的单元格)。所以,我稍微修改了拉斐尔的解决方案:

代码语言:javascript
复制
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryNone;
}

/*Here is modified part*/

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*
    ...
    Your implementation stays here
    we're just adding few lines to make sure
    that only correct rows will be selected
    */

    if([[tableView indexPathsForSelectedRows] containsObject:indexPath]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}
票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3040894

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档