UITableView委托方法调用了两次 [英] UITableView delegate method called twice

查看:135
本文介绍了UITableView委托方法调用了两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我的问题是关于UITableViewController-s
特别是我注意到数据源委托方法

Today my question is about UITableViewController-s In particular I have noticed that the datasource delegate method

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

被调用两次(即使我只是创建一个基于导航的应用程序,而不添加一行代码..加上一个NSLog跟踪它)。
现在,由于在我的应用程序中,我需要根据文件系统中的文档来确定部分的数量,所以我需要调用一些方法。我把这些方法放在上面提到的方法中,所以它们会被调用两次,这是我不需要的。
问题是为什么叫两次,我可以叫它一次吗?
我希望在官方文档中没有明确说明(这意味着我根本没有阅读:))。顺便说一句,我可以看到别人发表类似的问题,但我找不到一个完全令人满意的答案。
谢谢。

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). 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.

推荐答案

我遇到同样的问题,只有打电话给numberOfRowsInSection:
我收到的每个电话的答案都放在堆栈跟踪中。

I was experiencing the same problem, only with the call to numberOfRowsInSection: The answered laid in the stack trace for each call I received.


  1. 第一次调用是由于我在viewDidLoad中的表头视图发生了变化:我的viewcontroller。 / p>

  1. The first call was due to a change in the table header view I was making in the viewDidLoad: of my viewcontroller.

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

导致内部调用 _updateContentSize:,调用 heightForTable :,最终称为 numberOfRowsInSection:。这是我触发的,并且可以通过不执行上述代码来轻松避免:)

This resulted in internal call to _updateContentSize: which called heightForTable: which eventually called numberOfRowsInSection:. This was something I triggered, and could be easily avoided by not doing the above code :)

第二个调用是为了重新加载数据是合法的。这是由某个布局事件触发的,很可能您无法跳过。

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.

我确信你可以观察类似于numberOfSections的东西:方法

I'm sure you can observe something similar for the numberOfSections: method

所以,我的结论是,由于执行UITableView有许多情况下,某些委托方法将被调用两次或更多是因为表视图必须刷新一些东西。我试图围绕这个bug /功能/ etc设计我的代码。

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. I try to design my code around this bug/feature/etc.

希望它有助于

这篇关于UITableView委托方法调用了两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆