简体   繁体   English

在主线程上调用tableView.reloadData后,UITableView不刷新

[英]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. 更新:我对let vc = viewControllers(for: self)调用返回了错误的值,因此未触发更新。

I'm trying to update a UITableView with the tableView.reloadData method, but it's not triggering an update? 我正在尝试使用tableView.reloadData方法更新UITableView ,但是它没有触发更新?

The update should happen when selectAndHighlight is called from the parent view controller when a search result is clicked 单击搜索结果时,从父视图控制器调用selectAndHighlight时,应该进行更新

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. 单击搜索结果后,该扩展名最终调用SelectAndHighlight方法。

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() 在调用tableView.reloadData()之前尝试调用以下func

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. 问题是向RealmDB添加价值后,您需要获取更新的价值并再次更新Artist对象。

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

相关问题 在tableview.reloadData之后调用一次函数 - Calling a function once after tableview.reloadData 通过使用notificationcentre和tableview.reloaddata()无法刷新tableview - tableview not refreshing by using notificationcentre and tableview.reloaddata() 调用tableView.reloadData()时,“致命错误”表明tableView已正确连接 - “fatal error” when calling tableView.reloadData(), the tableView is properly connected UITableView tableView.reloadData()调用时没有更新,请问是什么原因导致的? - UITableView not updating when tableView.reloadData() is called, what is causing this problem? 在调用tableView.reloadData()之后,TableView无法重新加载 - TableView fail to reload after `tableView.reloadData()` is called 调用tableView.reloadData()删除自定义单元内动画 - Calling tableView.reloadData() removes custom in-cell animation TableView.reloadData()无法正常工作? (迅速) - TableView.reloadData() is not working ? (SWIFT) 方法 tableView.reloadData() 不起作用 - Method tableView.reloadData() not working tableView.reloadData()表未更新 - tableView.reloadData() Table not updating 通用函数不会在tableView.reloadData()之后更新tableview单元格中的label的值 - Generic function does not update the value of label in tableview cell after tableView.reloadData()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM