简体   繁体   中英

UITableView not refreshing after calling tableView.reloadData on main thread

Update: My call to let vc = viewControllers(for: self) was returning the wrong values, so the update wasn't triggering.

I'm trying to update a UITableView with the tableView.reloadData method, but it's not triggering an update?

The update should happen when selectAndHighlight is called from the parent view controller when a search result is clicked

Ive searched around, and from what I can tell, I needed to call the update on the main thread, so I've done that, but I'm still not getting any updates?

In the long term, I'll probably only want to update a single cell, but for now, I just want anything to update, which doesn't work. There is no log output from my print statement during the reload command.

When I scroll the content off the screen then back on, the new content is there correctly, so the data is sync'ing properly, it's just not updating.

import UIKit
import RealmSwift
import XLPagerTabStrip

class ArtistViewControllerTableViewController: UITableViewController, IndicatorInfoProvider {

open var stageName = ""

var artists: Results<Artist>!

override func viewDidLoad() {

    let realm = try! Realm()

    artists = try! realm.objects(Artist.self).filter("stageName = '" + stageName + "'")

    super.viewDidLoad()
}


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

    let cell = tableView.dequeueReusableCell(withIdentifier: "ArtistCell", for: indexPath)
    as! ArtistCellTableViewCell

    let artist = artists[indexPath.row] as Artist

    cell.artist = artist

    print ("updating " + artist.artistName)

    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("Artist Clicked")
}

open func selectAndHighlight(artist: Artist) {

    print (stageName)

    DispatchQueue.main.async {

        let realm = try! Realm()

        try! realm.write {
            artist.artistName = "new name"
        }

        self.tableView.reloadData()

    } 
}  

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return artists.count
}

func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
    return IndicatorInfo(title: stageName)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

The extension that ends up calling the SelectAndHighlight method after a search result is clicked.

extension ParentViewController: HandleSearchResultClick{
func select(artist: Artist) {
    print(artist.artistName + " clicked")
    var x = 0

    let vc = viewControllers(for: self)
    let table = vc[x] as? ArtistViewControllerTableViewController
    table?.selectAndHighlight(artist: artist)     
}
}

Try calling following func before calling tableView.reloadData()

func refreshArtistsObj(){
    let realm = try! Realm()
    artists = try! realm.objects(Artist.self).filter("stageName = '" + stageName + "'")
}

The problem is after adding value to RealmDB you need to fetch updated value and update your artists object again.

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