Swift:如何将单元格发送到TableView中的正确部分?

Swift:如何将单元格发送到TableView中的正确部分?,swift,uitableview,tableview,Swift,Uitableview,Tableview,我有简单的TableView和唯一的自定义单元格。 根据具体情况,我需要: 1.如果bool为真,则在第0节中显示单元格 2.如果bool为false,则在第1节中显示单元格 找不到正确的方法,需要帮助。 谢谢 这可以通过在表视图的数据源中实现适当的逻辑并在布尔值更改时在表视图上调用适当的更新方法来实现 基本示例: var showSomethingInSectionZero = true func numberOfSections(in tableView: UITableView) -&g

我有简单的TableView和唯一的自定义单元格。 根据具体情况,我需要:
1.如果bool为真,则在第0节中显示单元格
2.如果bool为false,则在第1节中显示单元格

找不到正确的方法,需要帮助。
谢谢

这可以通过在表视图的数据源中实现适当的逻辑并在布尔值更改时在表视图上调用适当的更新方法来实现

基本示例:

var showSomethingInSectionZero = true

func numberOfSections(in tableView: UITableView) -> Int {
    return 2
}

func numberOfRows(inSection section: Int) -> Int {
    if section == 0, showSomethingInSectionZero {
        return 1
    } else if section == 1, !showSomethingInSectionZero {
        return 1
    }
    return 0
}

func tableView(_ tableView: UITableView, 
  cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    return UITableViewCell()
}
并更新布尔值和表视图:

showSomethingInSectionZero = false
tableView.reloadData()
或者制作动画:

showSomethingInSectionZero = false
tableView.moveRow(at: IndexPath(row: 0, section: 0), 
      to: IndexPath(row: 0, section: 1))

这是它的基础,如果你想要更深入的答案,你应该提供更多的上下文和代码。

你可以关注cellForRow:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? customCell {
        if indexPath.section == 0 {
            cell.controlShowCell = controlShowCell
            return cell
        } else if indexPath.section == 1 {
            cell.controlShowCell = !controlShowCell
            return cell
        }
    }
    return UITableViewCell()
}

非常感谢,vector是正确的,但我仍然不理解。首先,我正确地制作了numberOfSections和numberOfRows。我对func tableView很感兴趣(tableView:UITableView,cellForRowAt indexPath:indexPath)。我需要cellForRowAt indexPath的实现如下:如果bool==true{为节0准备单元格}否则{为节1准备单元格}我终于理解了基本概念,再次感谢