简体   繁体   中英

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 ? when I switched UITableViewCell I want to update data reload change

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

 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

thank you for your help!

If you want to reload only particular tableView cell then use this:

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:

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

Where, <[IndexPath]> is the array of index path of the cells. 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.

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 . If you want to reload cell without animation then go with .none . You can also reload multiple cell in a tableView by providing IndexPath array. eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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