【问题标题】:UITableViewController numberOfRowsInSection doesn't affect the actual number of rowsUITableViewController numberOfRowsInSection 不影响实际行数
【发布时间】:2010-08-07 00:29:31
【问题描述】:

我遇到问题的表格是从另一个表格控制器推送的,并且该表格中只有一个部分,并且我正在使用自定义单元格。这是 numberOfRowsInSection 的代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger numberOfRows;
if ([imagesArray count] == 0) {
    numberOfRows = 0;
}
else {
    if ([imagesArray count] % 4 == 0) {
        numberOfRows = [imagesArray count] / 4;
    }
    else {
        numberOfRows = ([imagesArray count] / 4) + 1;
    }
}
NSLog(@"numberOfRowsInSection: %d", numberOfRows);
return numberOfRows;
}

这里的日志总是打印预期的行数。到现在为止还挺好。现在让我们看一下 cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"cellForRowAtIndexPath: %@", [indexPath description]);

    ImageTableCell *cell = (ImageTableCell *)[tableView dequeueReusableCellWithIdentifier:@"4ImagesCell"];
    if (cell == nil) {
            cell = [[[ImageTableCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"4ImagesCell"] autorelease];
    }
    return cell;
}

这里的控制台“总是”打印以下内容:

cellForRowAtIndexPath: <NSIndexPath 0x5a4f2c0> 2 indexes [0, 0]
cellForRowAtIndexPath: <NSIndexPath 0x5a4f2c0> 2 indexes [0, 1]
cellForRowAtIndexPath: <NSIndexPath 0x5a4f2c0> 2 indexes [0, 2]
cellForRowAtIndexPath: <NSIndexPath 0x5a4f2c0> 2 indexes [0, 3]
cellForRowAtIndexPath: <NSIndexPath 0x5a4f2c0> 2 indexes [0, 4]

无论 numberOfRowsInSection 返回的值是什么,无论是零还是 100,表格总是要求 5 行!我什至尝试在 numberOfRowsInSection 中返回一个静态数字而不是当前条件,但这对实际的单元格数量没有影响!

让我抓狂的是,表格在进入 cellForRowAtIndexPath 之前确实达到了 numberOfRowsInSection 方法!

我唯一能想到的可能是这个表控制器是从另一个 UITableViewController 推送的,但是我在这个表中时从不检查前一个表的 numberOfRowsInSection 方法,我什至尝试修改父表中的行没有任何运气。

如果缺少任何类型的信息,请告诉我。这个项目很复杂,我想不出任何其他相关信息来解决这个问题。

【问题讨论】:

    标签: iphone objective-c uitableview uitabbarcontroller


    【解决方案1】:

    那是因为 tableview 只加载可见单元格。你试过滚动你的表格视图吗?我敢打赌,如果你这样做,你会看到更多的行被加载...

    【讨论】:

    • 哦,是的,我第二天就知道了。下次我真的应该在困倦的时候停止编写/调试代码。感谢 Dave 和 Ankur 的友好回复。
    【解决方案2】:

    您可以尝试这样记录吗?

    NSLog(@"cellForRowAtIndexPath: (section:%@, row:%@)", [indexPath section], [indexPath row]);
    

    我认为我们所看到的描述方法的输出可能不是您认为的意思。

    【讨论】:

      猜你喜欢
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 1970-01-01
      • 2016-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多