简体   繁体   English

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

[英]How to reload tableViewCell only in swift?

Is there any way that I could reload only tableViewCell only but don't make it reload tableView section title View ?有什么办法可以只重新加载 tableViewCell 但不要让它重新加载 tableView 部分标题 View ? when I switched UITableViewCell I want to update data reload change当我切换 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

        }

What I want to do is just reload UITableViewCell only I don't want to reload this code below我想要做的只是重新加载 UITableViewCell 我不想重新加载下面的代码

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

if I use tableView.reload() it will effected my style in viewForHeaderInSection Because I have added UIViewScroll如果我使用 tableView.reload() 它会影响我在 viewForHeaderInSection 中的样式因为我添加了 UIViewScroll

thank you for your help!感谢您的帮助!

If you want to reload only particular tableView cell then use this: 如果您只想重新加载特定的tableView单元格,请使用以下命令:

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

It reload only cell not the section. 它仅重新加载单元,而不重新加载该部分。

重新加载特定的UITableViewCell

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

Look for the table view delegate methods for reloading views. 查找用于重新加载视图的表视图委托方法。 These are: 这些是:

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

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

open func reloadData()

Here in your case, reloadRows will be the best suitable option, as: 在您的情况下, reloadRows将是最合适的选项,如下所示:

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

Where, <[IndexPath]> is the array of index path of the cells. 其中, <[IndexPath]>是单元格的索引路径的数组。 For example: 例如:

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)

You can reload particular cell in UITableView with below code.您可以使用以下代码在UITableView重新加载特定单元格。

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

or或者

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

If you want to reload cell with default animation then go with .automatic .如果您想使用默认动画重新加载单元格,请使用.automatic If you want to reload cell without animation then go with .none .如果您想在没有动画的情况下重新加载单元格,请使用.none You can also reload multiple cell in a tableView by providing IndexPath array.您还可以通过提供IndexPath数组在tableView重新加载多个单元格。 eg例如

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

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

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