如何找出静态表视图单元属于哪个部分?

时间:2017-10-31 18:34:37

标签: ios swift uitableview

我有一个包含不同部分的静态表视图。在这里你可以看到其中一个部分:

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

tableView(_ didSelectRowAt:)

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

3 个答案:

答案 0 :(得分:1)

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

目前,该表更新标签以表示已选择,并自动将其他两个设置为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中定义的单个单元格。

答案 1 :(得分:0)

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)中,您可以使用indexPath.section获取该部分。将选择的indexPath存储在一个数组中,并在func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)内遇到一个部分时检查并替换indexPath。

像这样:

var selectedIndexPaths:[IndexPath] = []
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
   if let sameSectionPathIndex = selectedIndexPaths.index(where:{$0.section == indexPath.section}){
      if let previousSelectedCell = tableView.cellForRow(at:selectedIndexPaths[sameSectionPathIndex]){
         //Do unchecking and anything else with the previously selected cell
      }
      selectedIndexPaths.remove(at: sameSectionPathIndex)
      selectedIndexPaths.insert(indexPath, at: sameSectionPathIndex)
   } else {
      selectedIndexPaths.append(indexPath)
   }
   if let toBeSelectedCell = tableView.cellForRow(at: indexPath){
      // Do checking and anything else you want with the newly selected cell
   }
}

答案 2 :(得分:0)

你可以做到lufritz所说的。

在viewController中创建一个变量以获得所选索引

var selectedIndex = -1

您可以使用-1设置值,因为0是表格的第一个单元格。

我不知道你想如何制作选择效果,在这个例子中我只是改变了背景颜色,为选择创建了两个函数并取消选择了单元格:

func selectCell(cell:UITableViewCell){
    cell.backgroundColor = .green
}

func deselectCell(cell:UITableViewCell){
    cell.backgroundColor = .white
}

在您的cellForRowAt indexPath:方法中,您可以执行以下操作:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell"){
        cell.textLabel?.text = "Example \(indexPath.row)"

        deselectCell(cell: cell)

        if selectedIndex == indexPath.row{
            selectCell(cell: cell)
        }

        return cell
    }

    return UITableViewCell.init()

}

如果selectedIndex是当前indexPath.row,则使用selectCell方法,但首先必须使用取消选择方法,取消选择最后一个方法。

最后一件事是设置selectedIndex变量,就像你想的那样,把它放在didSelectRowAt indexPath方法中:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    selectedIndex = indexPath.row
    tableView.reloadData()

}