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

时间:2017-04-23 06:12:39

标签: ios swift uitableview

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

谢谢你的帮助!

3 个答案:

答案 0 :(得分:7)

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

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

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

答案 1 :(得分:2)

重新加载特定的UITableViewCell

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

答案 2 :(得分:1)

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

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)
相关问题