[英]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.