更新:我对let vc = viewControllers(for: self)
的调用返回了错误的值,因此更新未触发。
我正在尝试使用UITableView
方法更新tableView.reloadData
,但是它没有触发更新?
在单击搜索结果时从父视图控制器调用selectAndHighlight
时,应进行更新
从长远来看,我可能只想更新单个单元格,但就目前而言,我只想要更新任何内容,但这不起作用。在重新加载命令期间,我的print语句没有日志输出。
当我从屏幕上滚动内容然后重新开启时,新内容正确,因此数据正确同步,而不是更新。
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.
}
}
单击搜索结果后最终调用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)
}
}
答案 0 :(得分:0)
在调用tableView.reloadData()
func refreshArtistsObj(){
let realm = try! Realm()
artists = try! realm.objects(Artist.self).filter("stageName = '" + stageName + "'")
}
问题是在向RealmDB添加值后,您需要获取更新的值并再次更新您的artist对象。