我的项目中有一个 for 循环
:
for (int i = 0; i < 2; i++) {
[array addObject:randomObject];
[self.tableView reloadData];
}
我有这样的数据源方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"Count: %lu",(unsigned long)array.count);
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Reloading Data");
static NSString *CellIdentifier = @"Random";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
但是由于某些奇怪的原因,只有 numberOfRowsInSection
被调用而 cellForRowAtIndexPath
没有被调用。
此外,cellForRowAtIndexPath 确实在循环的最后一次被调用(当 x = 为 1 时)
最佳答案
tableview 仅在更新其 View 层次结构时请求单元格。在重新加载其数据时不请求。这只会导致它更新其内部大小信息。
仅当运行循环进行时更新单元格。当您处于循环中时,主线程被阻塞,然后运行循环不会进行。
只有在您的循环之后,操作系统才会运行 runloop 并且表格会调用 Cells。
关于ios - cellForRowAtIndexPath 未被调用但 numbersOfRowInSection 是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20193439/