ios - cellForRowAtIndexPath 未被调用但 numbersOfRowInSection 是

标签 ios for-loop uitableview

我的项目中有一个 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/

相关文章:

python - 在 python 中使用名称列表打开多个文件

php - 如何使用 php 每行显示四个图像并且行可以是任何 no

java - 编写一个程序,测试二维数组中是否有两个 1 位于同一行或同一列

ios - 在 Swift 中,检测 UITableViewCell 部分中的点击

ios - 如何将 preferredMaxLayoutWidth 用作我的 UILabel 的自动设置并找出其 View 的高度?

ios - 在 tableview 中每 5 个单元格后添加一个广告替换内容

ios - 通过单元格中的按钮通过 segue 传递数据……不工作……

ios - 具有 self 的 Swift 通用协议(protocol)功能

ios - 带有 NSAttributedString 和自定义属性的 UITextView 不起作用

iphone - 我想在接受用户输入的屏幕上弹出一个小 View (200x200)