繁体   English   中英

如何仅在 swift 中重新加载 tableViewCell?

[英]How to reload tableViewCell only in swift?

有什么办法可以只重新加载 tableViewCell 但不要让它重新加载 tableView 部分标题 View ? 当我切换 UITableViewCell 时,我想更新数据重新加载更改

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

        switch self[selectedIndex] {

        case .tracks:

        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! TracksCell
         return cell

        case .playlists:

          let cell = tableView.dequeueReusableCell(withIdentifier: cellPlayListId, for: indexPath) as! PlaylistCell

        return cell

        }

我想要做的只是重新加载 UITableViewCell 我不想重新加载下面的代码

 override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {} 

如果我使用 tableView.reload() 它会影响我在 viewForHeaderInSection 中的样式因为我添加了 UIViewScroll

感谢您的帮助!

如果您只想重新加载特定的tableView单元格,请使用以下命令:

tableView.reloadRows(at: [IndexPath(row: rowNumber, section: 0)], with: .automatic)

它仅重新加载单元,而不重新加载该部分。

重新加载特定的UITableViewCell

tableviewCart.reloadRows(at: [indexPath!], with: .none)

查找用于重新加载视图的表视图委托方法。 这些是:

open func reloadSections(_ sections: IndexSet, with animation: UITableView.RowAnimation)

open func reloadRows(at indexPaths: [IndexPath], with animation: UITableView.RowAnimation)

open func reloadData()

在您的情况下, reloadRows将是最合适的选项,如下所示:

tableView.reloadRows(at: <[IndexPath]>, with: .automatic) 

其中, <[IndexPath]>是单元格的索引路径的数组。 例如:

let cell0FromSection0 = IndexPath(row: 0, section: 0)
let cell2FromSection0 = IndexPath(row: 2, section: 0)
let cell1FromSection1 = IndexPath(row: 1, section: 1)

tableView.reloadRows(at: [cell0FromSection0, cell2FromSection0, cell1FromSection1], with: .automatic)

您可以使用以下代码在UITableView重新加载特定单元格。

let cellNmber = IndexPath(row: rowNumber, section: sectionNummber)
tableView.reloadRows(at: [cellNmber], with: .automatic)

或者

tableView.reloadRows(at: [cellNmber], with: .none)

如果您想使用默认动画重新加载单元格,请使用.automatic 如果您想在没有动画的情况下重新加载单元格,请使用.none 您还可以通过提供IndexPath数组在tableView重新加载多个单元格。 例如

tableView.reloadRows(at: [cellNmber1,cellNmber3,cellNmber5], with: .automatic)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM