轻按单元格时在另一个单元格中隐藏按钮

时间:2018-09-21 20:39:51

标签: swift uitableview

我有一个带有三个单元的`tableView。在其中两个中,我有一个按钮。当一个单元格被点击时,我想将按钮隐藏在另一个单元格中。

我在didSelectRowAt中尝试了一些解决方案,但无法隐藏按钮:

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 1 {
        if let cell = tableView.cellForRow(at: indexPath) as? ComposeCell1 {
           cell.compose.isHidden = true
        if let cell2 = tableView.cellForRow(at: indexPath) as? ComposeCell2 {
            cell2.compose.isHidden = true //I can reach the cell from here, but its not hiding the button
            }

            }
        } else if indexPath.row == 2 {
            if let cell = tableView.cellForRow(at: indexPath) as? ComposeCell2 {
                cell.compose.isHidden = true
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

尝试使用变量跟踪按下按钮的索引。并使用tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)-> UITableViewCell中的变量来更新按钮的可见性。在didSelectRowAt中,您唯一需要做的就是为索引值分配变量并重新加载表视图

var selectedIndex: Int?

....

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   .....
   cell.compose.isHidden = indexPath.row != selectedIndex
   .....
   return cell

}