ios - 如何找出静态 TableView 单元格属于哪个部分?

标签 ios swift uitableview

我有一个包含不同部分的静态 TableView 。您可以在此处看到其中一个部分:

用户可以点击这些部分中的单元格来检查它们,但我希望他们不能点击同一部分中的两个单元格,例如,如果他们点击了名为“Name A-Z”的单元格,那么他们点击名为“Name Z-A”的,第一个未被选中,第二个被选中。为此,我认为,我应该以某种方式检查两个单元是否来自同一部分,但我不知道如何实现。我正在使用

tableView(_ didSelectRowAt:)

选择单元格的方法,但我不知道如何从那里访问单元格的部分。也许我应该使用另一种方法?有什么想法如何实现这个吗?

最佳答案

好的,下面的代码可以让您在选择单元格时执行特定操作。代码也会自动识别它所在的部分。

当前,该表更新标签以表示已选择,并自动将其他两个设置为 N/A。您可以将其替换为您自己的代码以隐藏/显示复选标记或其他内容。

Here is a video下面的代码也做了什么。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    switch indexPath.section {
    case 0:
        switch indexPath.row {
        case 0:
            sectionOneCellOne.textLabel?.text = "SELECTED"
            sectionOneCellTwo.textLabel?.text = "N/A"
            sectionOneCellThree.textLabel?.text = "N/A"
        case 1:
            sectionOneCellOne.textLabel?.text = "N/A"
            sectionOneCellTwo.textLabel?.text = "SELECTED"
            sectionOneCellThree.textLabel?.text = "N/A"
        case 2:
            sectionOneCellOne.textLabel?.text = "N/A"
            sectionOneCellTwo.textLabel?.text = "N/A"
            sectionOneCellThree.textLabel?.text = "SELECTED"

        default:
            break
        }
    case 1:
        switch indexPath.row {
        case 0:
            sectionTwoCellOne.textLabel?.text = "SELECTED"
            sectionTwoCellTwo.textLabel?.text = "N/A"
            sectionTwoCellThree.textLabel?.text = "N/A"
        case 1:
            sectionTwoCellOne.textLabel?.text = "N/A"
            sectionTwoCellTwo.textLabel?.text = "SELECTED"
            sectionTwoCellThree.textLabel?.text = "N/A"
        case 2:
            sectionTwoCellOne.textLabel?.text = "N/A"
            sectionTwoCellTwo.textLabel?.text = "N/A"
            sectionTwoCellThree.textLabel?.text = "SELECTED"
        default:
            break
    }
    default:
        break
}

}

sectionOneCellOne 变量等只是在 View Controller 中定义的单个单元格。

关于ios - 如何找出静态 TableView 单元格属于哪个部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47042367/

相关文章:

ios - 如何以编程方式子类化 CollectionViewCell 并更改子类标识符?

ios - 在 iOS 9 中将 barButtonItem 用于 popoverPresentationController 时遇到问题?

ios - MapKit - 在不占用大量 CPU 的情况下跟踪 map 上的用户位置

Swift - 展开的字符串仍然显示为可选

ios - 如何检查 key 对中的 key 是否可用?

iphone - iPhone 开发中持久化数据的选项

ios - 在 Swift 中使用 Objective-C block 时保留参数类型

swift - 无法获得自动布局以使两个按钮并排留在那里

objective-c - 阻止导航 Controller 导航并要求用户确认

ios - UITableView 重新加载自定义 UITableViewHeaderFooterView 的数据