ios - Swift:多个 TableView 复选标记 - 选择所有行

标签 ios swift uitableview multi-select

是否可以在 TableView 中添加一个全选选项 多个复选标记...?

我准备好了代码,我尝试实现这个功能。请帮助我!

代码:

struct Area {
        let name : String
        var isSelected : Bool

        init(name : String, isSelected : Bool = false) {
            self.name = name
            self.isSelected = isSelected
        }
    }

    var areas = [Area(name: "Select all"),Area(name: "a"),Area(name: "b"),Area(name: "c"),Area(name: "d"]

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return areas.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        let area = areas[indexPath.row]
        cell.textLabel?.text = area.name
        cell.accessoryType = area.isSelected ? .checkmark : .none

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        areas[indexPath.row].isSelected.toggle()
        tableView.reloadRows(at: [indexPath], with: .none)
        let selectedAreas = areas.filter{$0.isSelected}.map{$0.name}
        print(selectedAreas)

    }

谢谢。

最佳答案

您必须检查用户是否选择了第一行(“全选”)并相应地更新其他行:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // toggle the selected area
    areas[indexPath.row].isSelected.toggle()

    // save the new state for later use
    let isSelected = areas[indexPath.row].isSelected

    if indexPath.row == 0 {
        // "Select all" was selected – update all areas
        for i in 1..<areas.count {
            areas[i].isSelected = isSelected
        }

        // update UI
        tableView.visibleCells.forEach { $0.accessoryType = isSelected ? .checkmark : .none }
    } else {
        // update UI
        tableView.cellForRow(at: indexPath)?.accessoryType = isSelected ? .checkmark : .none
    }

    tableView.deselectRow(at: indexPath, animated: true)
}

推荐

为了在视觉上分离关注点,您还可以为“全选”行使用自己的表格 View 部分。在这种情况下,需要进行更多更改:

var areas = [
    // you do not need an area for "Select all" any longer
    Area(name: "a"),
    Area(name: "b"),
    Area(name: "c"),
    Area(name: "d")
]

var allSelected: Bool {
    // helper var to see if all areas are currently selected
    return areas.filter({!$0.isSelected}).isEmpty
}

override func numberOfSections(in tableView: UITableView) -> Int {
    return 2
}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    switch section {
    case 1: return "Areas"
    default: return nil
    }
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch section {
    case 0: return 1 // select all
    case 1: return areas.count
    default:
        // we should never get here
        fatalError()
    }
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.selectionStyle = .none

    if indexPath.section == 0 {
        cell.textLabel?.text = "Select all"
        cell.accessoryType = allSelected ? .checkmark : .none
    } else {
        let area = areas[indexPath.row]
        cell.textLabel?.text = area.name
        cell.accessoryType = area.isSelected ? .checkmark : .none
    }

    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.section == 0 {
        // (de-)select all
        let shouldSelect = !allSelected
        for i in 0..<areas.count {
            areas[i].isSelected = shouldSelect
        }
    } else {
        areas[indexPath.row].isSelected.toggle()
    }

    tableView.reloadRows(at: tableView.indexPathsForVisibleRows ?? [], with: .automatic)
}

关于ios - Swift:多个 TableView 复选标记 - 选择所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57986671/

相关文章:

swift - 如何将相机按钮添加到 UIImageView

使用 x-callback-url 的 iOS Interapp 双向通信

ios - 在 Swift 中实现惰性属性并将其设置为 nil

ios - 如何在 iOS 中滑动 UITableViewCell

ios - 如何为具有 0 到 2 个图像的 UITableViewCells 设置可调整高度的约束?

objective-c - Xcode 4 Interface Building字体未更新/显示

ios - Xcode Swift 参数类型不符合预期类型 'Decoder'

ios - NSData 如何存储在 .plists 中?

ios - UITableViewController 类似于 iOS7 日历应用程序的添加事件 TableView Controller

ios - 在 ViewController iOS 之前加载 DataController