UITableView多项选择

Han*_*kke 42 iphone uitableview

如何在我的基于视图的应用程序中添加UITableView,用户将点击多个单元格,它将被选中,就像Clock应用程序的"New Alarm"设置名为"Repeat"(Clock> Alarms> +>重复),我怎样才能获得数组中的所有选定单元格?

Rap*_*ira 129

要进行多项选择,请在下方添加以下行 viewDidLoad()

tableView.allowsMultipleSelection = true
Run Code Online (Sandbox Code Playgroud)

cell在将其出列(或初始化)之后配置每个tableView(_:cellForRowAt:)

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
Run Code Online (Sandbox Code Playgroud)

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

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
}
Run Code Online (Sandbox Code Playgroud)

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

let selectedRows = tableView.indexPathsForSelectedRows
Run Code Online (Sandbox Code Playgroud)

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

let selectedData = selectedRows?.map { dataArray[$0.row].ID }
Run Code Online (Sandbox Code Playgroud)

  • 我不明白这是为什么不接受的答案...如果您使用的是自定义单元格,你也可以改变外观在你的细胞的的setSelected:动画:基于细胞的状态的方法 (9认同)
  • 获取所选对象的索引NSArray*selectedCells = [self.tableView indexPathsForSelectedRows]; (3认同)

Bre*_*erg 28

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];

    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    } else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
}
Run Code Online (Sandbox Code Playgroud)

除了单元格自己的附件类型状态之外,您还可以维护一组选定状态,或者在表视图中迭代查询每个状态的单元格以读取所选行.

  • 这不起作用 - 当选择单元格并滚动时,indexPath引用了错误的单元格.因此,您将看到已检查多个项目. (15认同)
  • -1此解决方案存在问题,一旦选择了某些行并滚动,您可以看到许多尚未选择的单元格的复选标记. (2认同)

Dmi*_*sev 18

@BrendanBreg 实现对我没用.@RaphaelOliveira提供了很好的解决方案,但是当您向下滚动表格时 - 选择了错误的行(因为UITableView缓存了它的单元格).所以,我稍微修改了拉斐尔的解决方案:

- (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;
    }
}
Run Code Online (Sandbox Code Playgroud)


Ham*_*mdi 15

self.tableView.allowsMultipleSelection = YES;
Run Code Online (Sandbox Code Playgroud)


sou*_*ael 14

除了上面的答案之外,还有一个快速提示:从时钟应用程序模仿Apple的风格(在检查/取消选中行后使行选择颜色淡出),didSelectRowAtIndexPath在条件之后将其添加到:

[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
Run Code Online (Sandbox Code Playgroud)

来自Apple的TableMultiSelect指南.