【问题标题】:How to insert one Cell into the first section of UITableView Swift如何将一个 Cell 插入 UITableView Swift 的第一部分
【发布时间】:2020-12-23 19:40:49
【问题描述】:

我创建了一个包含两个部分的 UITableView。现在我正在尝试填充一个部分。当我执行操作时,我的程序会同时使用相同的文本填充两个部分。我要做的是填充第一部分,然后最终将其中一个单元格从第 1 部分移动到第 2 部分。到目前为止,我到了这里:

extension ListVC: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    randomCount = notes.count
    return notes.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: CustomNoteCell.reuseId) as! CustomNoteCell
    cell.set(with: notes[indexPath.row])
    return cell

}

func numberOfSections(in tableView: UITableView) -> Int {
    sections.count
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if section < sections.count {
        return sections[section]
    }
    return nil
}

}

我相信它需要在 cellForRowAt 中发生,但我不知道如何指定部分。任何帮助和线索表示赞赏。非常感谢;)

【问题讨论】:

    标签: swift xcode uikit


    【解决方案1】:

    numberOfRowInSection的代码改成这样:

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 { //first section
            randomCount = notes.count
            return notes.count
        }else{
        return 0
        }
    }
    

    【讨论】:

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