【问题标题】:How to find out which section the static table view cell belongs to?如何找出静态表格视图单元格属于哪个部分?
【发布时间】:2018-04-28 05:21:49
【问题描述】:

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

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

tableView(_ didSelectRowAt:)

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

【问题讨论】:

  • 这些是静态单元格是吗?当一个被选中时,您可以使用标签并禁用另外两个单元格。如果您愿意,我可以提供一个答案来说明如何做到这一点。
  • 您可以在视图控制器中为该部分使用 selectedIndex 变量,如果它发生更改,则重新加载该部分。
  • @CaseyWest 那太好了。谢谢
  • @lufritz 你所说的“视图控制器中的 selectedIndex 变量该部分”是什么意思?我应该创建一个变量来跟踪单元格属于哪个部分,或者什么?你能解释一下那部分吗?
  • 如果您对我的回答有任何疑问,请告诉我。 @TigranIskandaryan

标签: ios swift uitableview


【解决方案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 变量等只是在视图控制器中定义的单个单元格。

【讨论】:

  • 我的实现有点不同(我添加了一个复选标记并更改了单元格的 isSelected 属性),但方法是相同的。谢谢。
【解决方案2】:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 中,您可以使用indexPath.section 获取该部分。将选择的 indexPaths 存储在一个数组中,并在 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
   }
}

【讨论】:

    【解决方案3】:

    你完全可以按照 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 方法,但首先必须使用deselect 方法,以取消选择最后一个。

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

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
        selectedIndex = indexPath.row
        tableView.reloadData()
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-19
      相关资源
      最近更新 更多