[英]iOS: cellForRowAtIndexPath not being called immediately after numberOfRowsInSection
I have my uitable data being populated for the current section inside numberOfRowsInSection
. 我在
numberOfRowsInSection
的当前部分填充了我的应有数据。 I then expect it to call cellForRowAtIndexPath
so that I can use that data. 然后,我希望它调用
cellForRowAtIndexPath
以便我可以使用该数据。
In my numberOfRowsInSection
I am overwriting the data from the previous section. 在我的
numberOfRowsInSection
我正在覆盖上一节中的数据。 Doing NSLog
it shows that numberOfRowsInSection
runs for all 149 sections before the first cellForRowAtIndexPath
is called... Why? 通过执行
NSLog
它显示在调用第一个cellForRowAtIndexPath
之前,所有149个节均运行numberOfRowsInSection
。为什么?
I have removed calls to reloadData
since I saw it was the issue for someone else here on stack overflow but that didn't resolve the issue. 我已经删除了对
reloadData
调用,因为我在堆栈溢出时看到了这对其他人的问题,但这不能解决问题。
Per Jim I am adding what I am doing. Per Jim,我要添加我正在做的事情。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSMutableArray *tempArray = [NSMutableArray array];
FMResultSet *appointmentResults = [[DataClass getDB] executeQuery:@"SELECT * FROM Appointments WHERE date = ?",[self.appointmentDates objectAtIndex:section]];
int rowCount = 0;
while ([appointmentResults next]) {
[tempArray addObject:[appointmentResults resultDict]];
rowCount++;
}
self.appointments = tempArray;
NSLog(@"Appointments: %@ - %i", [self.appointmentDates objectAtIndex:section], rowCount);
return rowCount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Parse the appointments for the current section and display them
}
In a general sense, it's not very good practice to rely upon Apple calling your delegate functions in any particular pattern. 一般而言,依靠Apple以任何特定模式调用委托函数并不是一个很好的做法。
With UITableView in particular, it only will request data on a need-to-know basis. 特别是对于UITableView,它只会在需要知道的基础上请求数据。 The UITableView has a greater need to know how many sections and rows there are in the entire table so that it can handle things like scrolling position and scrollbar size properly, while actual cell contents can be allowed to slide until they're about to enter the user's view.
UITableView更加需要知道整个表中有多少节和行,以便它可以正确处理诸如滚动位置和滚动条大小之类的事情,而实际的单元格内容可以被允许滑动,直到它们即将进入用户视图。
You can't take a call to numberOfRowsInSection as implying anything more than that the system wants to know how many rows are in a section. 您不能对numberOfRowsInSection进行调用,这暗示着系统想要知道某节中有多少行之外,还没有任何其他含义。 :)
:)
Its the behavior of UITableView. 它是UITableView的行为。
It will load only those cells which are visible. 它将仅加载那些可见的单元格。 To calculate it, it will ask number of rows for each sections before displaying them.
为了计算它,它将在显示每个部分之前询问每个部分的行数。
The cells which are not visible, cellForRowAtIndexPath
will not get called initially. 不可见的单元格
cellForRowAtIndexPath
最初不会被调用。 It will get called once user scrolls through it. 用户滚动浏览它时,它将被调用。
I also get the same situation. 我也遇到同样的情况。 i think the reason is that the heightof row return by the table is 0.
我认为原因是表格返回的heightof行为0。
我的猜测是uitableview会缓存数据,因此运行一次并获取所有内容
It is how the UITableView
class works.. 这就是
UITableView
类的工作方式。
Nothing to be shocked at..and you won't be able to change that.. 没什么好震惊的,您将无法更改它。
Also 149 sections should not be loaded at once.. load them as the user scrolls.. 另外,不应一次加载149个部分。随着用户滚动滚动它们。
If you want to refresh/change your table you should change appropriate data in model
, then call one of tableView's [tableView reload....];
如果要刷新/更改表,则应在
model
更改适当的数据,然后调用tableView的[tableView reload....];
methods (depends on what exactly you're trying to change). 方法(取决于您要更改的内容)。
UITableViewDelegate
methods (especially those returning number of rows, sections...) shouldn't do much more than their name says or you'll end up with a total mess of your table or possibly app crashing... UITableViewDelegate
方法(尤其是那些返回行数,节数的方法)的作用不应该超过它们的名称,否则您将导致表格完全混乱或应用程序崩溃……
When you call [tableView reload....];
当您调用
[tableView reload....];
your content-providing methods will get called and that's the place to provide, well, content. 您提供内容的方法将被调用,这就是提供内容的地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.