swift - UITableView - 多选和单选

标签 swift uitableview xcode7

我的 UITableView 中有 2 个部分。
我希望第一部分允许选择多个单元格,而第二部分只允许单选。
我尝试了一些代码,但效果不是很好。
如果可能,请快速编码。谢谢。

enter image description here

最佳答案

您可以简单地尝试一下。这个解决方案非常适合我。试一试也许对其他人有用...

swift -4

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.section == 0 {
        if let cell = tableView.cellForRow(at: indexPath) {
            cell.accessoryType = .checkmark
        }
    }
    else {
        if let cell = tableView.cellForRow(at: indexPath) {
            cell.accessoryType = .checkmark
        }
    }
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    if indexPath.section == 1 {
        if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
            cell.accessoryType = .none
        }
    }
}

关于swift - UITableView - 多选和单选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32857091/

相关文章:

ios - 使用 objectsWhere() 查询 Realm 数据库时的 CVaListPointer

ios - 将导航栏放在 UISearchController 前面,Swift

ios - 当单元格插入第一个位置并选择底部单元格时,UITableView 分隔线消失

ios - 无法在 SpriteKit Swift 中使用 SKVideoNode 播放视频

xcode - 类型 'ViewController' 不符合协议(protocol) 'UIDocumentPickerDelegate' swift 2

ios - Swift 3 中的 Firebase 多级数据库处理

ios - SwiftUI 按钮路径问题 : unrecognized selector error pops up when ran

swift - El Capitan XCode7.0.1 和 XCode7.1Beta 中的 Playground 错误

iphone - 动态添加 UITableViewCell 和 UITextField

ios - 以编程方式将 admob View 添加到 UITableView 的顶部或底部?