简体   繁体   English

在调用tableView.reloadData()之后,TableView无法重新加载

[英]TableView fail to reload after `tableView.reloadData()` is called

I have two tabs. 我有两个标签。 The first tab contains a UITableViewController , with its tableView's cells containing some words. 第一个选项卡包含一个UITableViewController ,其tableView的单元格包含一些单词。 The second tab is for settings, containing a tableView, with a cell for setting font size. 第二个选项卡用于设置,包含一个tableView以及一个用于设置字体大小的单元格。 After the user taps the cell, the user will enter the interface for changing the font size. 用户点击单元格后,用户将进入更改字体大小的界面。 When the user presses the "save" button, (a button in the editing-font-size interface to save the adjusted font size.) the top view controller is popped back to the original tableView for settings. 当用户按下“保存”按钮(“编辑字体大小”界面中的一个按钮以保存调整后的字体大小。)时,顶视图控制器会弹出回到原始tableView进行设置。

I use observers to post notification when the save button is pressed, and the respective observers will update its view. 当按下保存按钮时,我使用观察者发布通知,各个观察者将更新其视图。 I added an observer in the tableView for settings, and it works. 我在tableView中添加了一个观察者进行设置,并且它可以工作。 When the top view controller for editing font size is popped, the original tableView is presented with adjusted font size. 弹出用于编辑字体大小的顶视图控制器时,原始tableView会显示调整后的字体大小。

However, when I added an observer in the UITableViewController in the first tab, the tableView is not reloaded. 但是,当我在第一个选项卡的UITableViewController中添加观察者时,不会重新加载tableView。 I have added a print message in the selector method of NotificationCenter.default.addObserver(_:selector:name:object:) and confirm that the selector method is called. 我已经在NotificationCenter.default.addObserver(_:selector:name:object:)的选择器方法中添加了打印消息,并确认选择器方法已被调用。 The selector method looks like this: 选择器方法如下所示:

@objc func reloadTableView() {
    print("The method is called.")
    tableView.reloadData()
}

The code that I use the font size in the first tab's tableView cell: 我在第一个选项卡的tableView单元格中使用字体大小的代码:

labelOne.font = UIFont(descriptor: fontDescriptor, size: fontSizeForLabelOne)
labelTwo.font = UIFont(descriptor: fontDescriptor, size: fontSizeForLabelTwo)

It seems very confusing to me, since the method to reload tableView is called, yet the table view is not reloaded? 对我来说,这似乎很令人困惑,因为调用了重载tableView的方法,但是还没有重载表视图? And since the font size is changed, when I scroll the tableView the new cells are randomly in the old font size or adjusted font size. 而且由于字体大小已更改,因此当我滚动tableView时,新单元格将随机以旧字体大小或调整后的字体大小显示。

I have also tried it with labels, and it works on them even if it is in a different tab. 我也尝试过使用标签,即使在其他标签中也可以在标签上使用。 In other words, I think the problem only occurs for tableView. 换句话说,我认为问题仅发生在tableView上。

On the other hand, since it works for the table view in setting tab, am I right to say that tableView can only be reloaded when tableView.reloadData() is called when the tableView is about to be presented? 另一方面,由于它适用于“设置”选项卡中的表视图,因此我是否说仅当要显示tableView.reloadData()时调用tableView.reloadData()时才能重新加载tableView.reloadData() If tableView is not presented and tableView.reloadData() is called, nothing will happen, and everything remains the same? 如果未提供tableView.reloadData()并调用tableView.reloadData() ,将不会发生任何事情,并且一切都保持不变?

In short, is there anyway to ensure that tableView is reloaded when I change my font size? 简而言之,是否有任何方法可以确保在更改字体大小时重新加载tableView?

try this 尝试这个

@objc func reloadTableView() {
    print("The method is called.")
     DispatchQueue.main.async {
           tableView.reloadData()
        }

}

Hope this will work. 希望这会起作用。

Eventually I found a solution by, since my view controllers are managed by navigation controller, I pop the view controller and load it again as the alternative to reload the table view. 最终,我找到了一个解决方案,因为我的视图控制器是由导航控制器管理的,所以我弹出视图控制器并再次加载它,作为重新加载表格视图的替代方法。 Namely,

@objc func reloadTableView() {
    navigationController.popViewController(animated: false)
    navigationController.pushViewController(theViewController, animated: false)
}

The only downside of this is that the view controller will be reset to its initial state, ie if the table view in the view controller is scrolled down, when the user goes back to the view controller from setting, the table view is reset to its top position and does not show where it was scrolled to. 唯一的缺点是视图控制器将重置为初始状态,即,如果向下滚动视图控制器中的表视图,则当用户从设置返回到视图控制器时,表视图将重置为其初始状态。最高位置,并且不显示滚动到的位置。

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

相关问题 tableView.reloadData()仅调用一次 - tableView.reloadData() only called once 在tableview.reloadData之后调用一次函数 - Calling a function once after tableview.reloadData tableView.reloadData() 添加新单元格而不是重新加载数据 - tableView.reloadData() add new cell instead of reload the data 为什么在我保存数据数据并调用 tableview.reloadData() 函数后我的 tabe 视图不会重新加载 - Why wont my tabe view reload after i save data data and call the tableview.reloadData() function Swift tableView.reloadData() 不会在 Swift 中重新加载表视图 - Swift tableView.reloadData() does not reload table view in Swift 应该在开始的tableview.reloadData之后仅对可见单元格调用cellForRowAt吗? - Should cellForRowAt gets called only for visible cells after tableview.reloadData at the beginning? TableView.reloadData()无法正常工作? (迅速) - TableView.reloadData() is not working ? (SWIFT) 方法 tableView.reloadData() 不起作用 - Method tableView.reloadData() not working tableView.reloadData()表未更新 - tableView.reloadData() Table not updating UITableView tableView.reloadData()调用时没有更新,请问是什么原因导致的? - UITableView not updating when tableView.reloadData() is called, what is causing this problem?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM