[英]UITableView numberOfSectionsInTableView and numberOfRowsInSection called thrice on initial load
I am making a REALLY BASIC UITableView
project without any data source. 我正在制作一个没有任何数据源的REALLY BASIC UITableView
项目。 I am just displaying "1" on first and only cell. 我只是在第一个也是唯一的单元格上显示“ 1”。
I am returning 1
in both numberOfSectionsInTableView
and numberOfRowsInSection
. 我在numberOfSectionsInTableView
和numberOfRowsInSection
中都returning 1
。 I am also doing an NSLog
on both methods simply stating the name of the respective methods. 我也在两个方法上做一个NSLog
,只是简单地说明了各个方法的名称。
On initial load, I get this: 在初始加载时,我得到以下信息:
SomeShit[27511:936083] number of sections
SomeShit[27511:936083] number of rows in section
SomeShit[27511:936083] number of sections
SomeShit[27511:936083] number of rows in section
SomeShit[27511:936083] number of sections
SomeShit[27511:936083] number of rows in section
Why are these methods calling thrice? 为什么这些方法调用三次? Correct me if I am wrong but this is really weird. 如果我错了,请纠正我,但这真的很奇怪。 I thought they were supposed to call only once because of number of items in both being just 1. 我以为他们应该只打一次电话,因为两者的数量都只有1。
EDIT: 编辑:
What I am doing in viewDidLoad
, 我在viewDidLoad
正在做什么,
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
Nowhere in the documentation does it say these methods are only call once in a reload data cycle. 文档中没有任何地方说这些方法在重载数据周期中仅被调用一次。
You've made the assumption that they only need to be called once, but as you have shown, that's a bad assumption. 您已经假设它们只需要被调用一次,但是正如您所显示的那样,这是一个错误的假设。
After reviewing the documentation for UITableView
and UITableViewDataSource
, the only promise is this. 在查看UITableView
和UITableViewDataSource
的文档后,唯一的保证就是这样。
-[UITableView numberOfSections]
: -[UITableView numberOfSections]
:
Discussion 讨论区
UITableView gets the value returned by this method from its data source and caches it. UITableView从其数据源获取此方法返回的值并将其缓存。
This means you can't know how calls to -[UITableView numberOfSections]
will correspond with calls to -[UITableViewDataSource numberOfSectionsInTableView:]
. 这意味着您不知道对-[UITableView numberOfSections]
调用将如何与对-[UITableViewDataSource numberOfSectionsInTableView:]
调用相对应。
Worse, the number of times these methods are called can (and have) change between different versions of iOS. 更糟糕的是,这些方法被调用的次数可能(并且已经)在不同版本的iOS之间改变。
In short, make these methods idempotent and performant , and forget about implementation details of UITableView
. 简而言之,使这些方法成为幂等和高性能 ,而UITableView
实现细节。
The delegate and data source are there to service the table view implementation, and methods may be called multiple times. 委托和数据源在那里服务于表视图实现,并且方法可以多次调用。 You should not concern yourself with the implementation details of table views, just make sure your answers are consistent (ie you can answer correctly at any point in time and answers do not change without calling reloadData
). 您不必担心表视图的实现细节,只需确保您的回答是一致的即可(即您可以在任何时间正确回答,并且在不调用reloadData
情况下答案不会改变)。
You could try to figure out what is causing these reloads. 您可以尝试找出导致这些重新加载的原因。 Go to breakpoints section (1), press on "plus" button (2). 转到断点部分(1),按“加号”按钮(2)。 Then add symbolic breakpoint with -[UITableView reloadData] in "symbol" field. 然后在“符号”字段中使用-[UITableView reloadData]添加符号断点。
Then run your app and see the stack trace of method calls. 然后运行您的应用程序,并查看方法调用的堆栈跟踪。 You will be able to figure out the reason of such behaviour. 您将能够弄清楚这种行为的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.