简体   繁体   English

UITableView委托方法调用两次

[英]UITableView delegate method called twice

Today my question is about UITableViewController-s In particular I have noticed that the datasource delegate method 今天我的问题是关于UITableViewController-s特别是我注意到了datasource委托方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

is called twice (even if for instance I just create a navigation based application and without adding a line of code.. well adding an NSLog to track it). 被调用两次(即使例如我只是创建一个基于导航的应用程序而不添加一行代码..添加一个NSLog来跟踪它)。 Now, since in my application I need to determine the number of sections basing the choice on the documents in the file system, I need to call some methods to do so. 现在,因为在我的应用程序中,我需要确定基于文件系统中文档选择的部分数量,我需要调用一些方法来实现。 I have put these methods in the above mentioned method, so they will be called twice, which is something I don't need. 我已经将这些方法放在上面提到的方法中,因此它们将被调用两次,这是我不需要的。 The questions are why is it called twice, can I have it called once? 问题是为什么它被调用两次,我可以调用一次吗? I hope that in the official documentation this is not clearly stated (which would mean that I didn't read it at all :) ). 我希望在官方文档中没有明确说明(这意味着我根本不读它:))。 By the way I could see others posting similar questions, but I couldn't find a fully satisfying answer. 顺便说一句,我可以看到其他人发布类似的问题,但我找不到一个完全令人满意的答案。 Thank you. 谢谢。

I was experiencing the same problem, only with the call to numberOfRowsInSection: The answered laid in the stack trace for each call I received. 我遇到了同样的问题,只是调用了numberOfRowsInSection:对于我收到的每个电话,答案都放在了堆栈跟踪中。

  1. The first call was due to a change in the table header view I was making in the viewDidLoad: of my viewcontroller. 第一个调用是由于我在viewDidLoad中生成的表头视图的更改:我的viewcontroller。

     thumbView.tableHeaderView = nil; thumbView.tableFooterView = nil; 

    This resulted in internal call to _updateContentSize: which called heightForTable: which eventually called numberOfRowsInSection: . 这导致对_updateContentSize的内部调用调用heightForTable:最终调用numberOfRowsInSection : . This was something I triggered, and could be easily avoided by not doing the above code :) 这是我触发的内容,可以通过不执行上述代码轻松避免:)

  2. The second call was the legitimate one in order to reloadData. 第二个调用是为了重新加载数据的合法调用。 This is triggered by a layout event somewhere and most likely you can't skip it. 这是由布局事件触发的,很可能你不能跳过它。

I'm sure you can observe something similar for the numberOfSections: method 我相信你可以观察到类似于numberOfSections:方法的东西

So, my conclusion is that due to the the implementation of UITableView there are many situations where certain delegate methods will get called twice or more because the table view has to refresh something. 因此,我的结论是,由于UITableView的实现,有许多情况下某些委托方法将被调用两次或更多次,因为表视图必须刷新一些东西。 I try to design my code around this bug/feature/etc. 我尝试围绕这个bug / feature / etc设计我的代码。

Hope it helps 希望能帮助到你

If your tableview is contained by a child view controller, Try this at your parent ViewController 如果您的tableview包含在子视图控制器中,请在父ViewController中尝试此操作

[parentViewController addChildViewController:childViewController];

before [parentViewController.view addSubview:childViewController.view] [parentViewController.view addSubview:childViewController.view]

请检查你的代码,添加后TableView你现在又可以称为realodData在MEY表的方法ViewWillAppear的方法

如果您的表格视图的框架在故事板中被错误更改,则会发生这种情况。您点击故事板上已将表格视图添加为子视图,现在您的表格可能没有您设置的正确框架在一开始的时候。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM