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

查看:31
本文介绍了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. 第一次调用是由于我在 viewcontroller 的 viewDidLoad: 中所做的表头视图发生了变化.

  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 的实现,在很多情况下某些委托方法将被调用两次或更多次,因为表视图必须刷新某些内容.我尝试围绕这个错误/功能/等设计我的代码.

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天全站免登陆