【问题标题】:How to reload tableViewCell only in swift?如何仅在 swift 中重新加载 tableViewCell?
【发布时间】:2017-04-23 06:12:38
【问题描述】:

有什么方法可以只重新加载 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

感谢您的帮助!

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

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

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

    它只重新加载单元格而不是部分。

    【讨论】:

    • 什么是 rowNumber ?
    • @Kristoff 声明是 func reloadRows(at indexPaths: [IndexPath], with animation: UITableViewRowAnimation),其中 indexPath 是标识要重新加载的行的 NSIndexPath 对象数组。
    • @Kristof rowNumber 表示您要重新加载的单元格数量。如果你想重新加载 2 号单元格,那么你可以这样写。 tableView.reloadRows(at: [IndexPath(row: 2, section: 0)], with: .automatic)
    • rowNumber 本质上是foo_rowNumber
    【解决方案2】:

    重新加载特定的UITableViewCell

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

    【讨论】:

    • 如果你想重新加载表格视图单元格
    【解决方案3】:

    寻找重新加载视图的表视图委托方法。它们是:

    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) 
    

    其中,&lt;[IndexPath]&gt; 是单元格的索引路径数组。例如:

    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)
    

    【讨论】:

      【解决方案4】:

      您可以使用以下代码重新加载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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-12-24
        • 2017-11-05
        • 1970-01-01
        • 1970-01-01
        • 2021-10-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多