iOS:cellForRowAtIndexPath 在 numberOfRowsInSection 之后没有立即调用
我已为 numberOfRowsInSection
内的当前部分填充了可用数据。然后我希望它调用 cellForRowAtIndexPath 以便我可以使用该数据。
在我的 numberOfRowsInSection
中,我将覆盖上一节中的数据。执行 NSLog 会显示,在调用第一个 cellForRowAtIndexPath
之前,numberOfRowsInSection
会运行所有 149 个部分...为什么?
我删除了对 reloadData
的调用,因为我发现这是其他人在堆栈溢出时遇到的问题,但这并没有解决问题。
根据吉姆的说法,我正在添加我正在做的事情。
- (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
}
I have my uitable data being populated for the current section inside numberOfRowsInSection
. I then expect it to call cellForRowAtIndexPath
so that I can use that data.
In my numberOfRowsInSection
I am overwriting the data from the previous section. Doing NSLog
it shows that numberOfRowsInSection
runs for all 149 sections before the first cellForRowAtIndexPath
is called... Why?
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.
Per Jim I am adding what I am doing.
- (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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
一般来说,依赖 Apple 以任何特定模式调用委托函数并不是很好的做法。
特别是对于 UITableView,它只会在需要知道的基础上请求数据。 UITableView更需要知道整个表格中有多少个部分和多少行,以便它可以正确处理诸如滚动位置和滚动条大小之类的事情,而可以允许实际单元格内容滑动直到它们即将进入表格用户的观点。
您不能将对 numberOfRowsInSection 的调用视为暗示系统想要知道一个部分中有多少行。 :)
In a general sense, it's not very good practice to rely upon Apple calling your delegate functions in any particular pattern.
With UITableView in particular, it only will request data on a need-to-know basis. 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.
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. :)
它是 UITableView 的行为。
它只会加载那些可见的单元格。为了计算它,它会在显示之前询问每个部分的行数。
对于不可见的单元格,
cellForRowAtIndexPath
最初不会被调用。一旦用户滚动它就会被调用。Its the behavior of 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. It will get called once user scrolls through it.我的猜测是 uitableview 缓存了该数据,因此运行一次并获取所有内容
my guess is that uitableview caches that data so runs through once and gets everything
这就是
UITableView
类的工作方式..没有什么值得震惊的..你将无法更改它..
也不应该一次加载 149 个部分..将它们加载为用户滚动..
It is how the
UITableView
class works..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..
如果您想刷新/更改表格,您应该更改
model
中的适当数据,然后调用 tableView 的[tableView reload....];
方法之一(取决于什么)正是你正在尝试改变)。UITableViewDelegate
方法(尤其是那些返回行数、节数的方法...)不应执行比其名称所示更多的操作,否则您最终会导致表完全混乱或可能导致应用程序崩溃。 ..当您调用
[tableView reload....];
您的内容提供方法将被调用,这就是提供内容的地方。If you want to refresh/change your table you should change appropriate data in
model
, then call one of tableView's[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...When you call
[tableView reload....];
your content-providing methods will get called and that's the place to provide, well, content.我也遇到同样的情况。我认为原因是表返回的行高为0。
I also get the same situation. i think the reason is that the heightof row return by the table is 0.