【发布时间】:2010-10-27 23:47:42
【问题描述】:
我一直在研究与 UITableView 和 cellForRowAtIndexPath 相关的问题。
我想让用户能够拥有非常多的行,就像 iPhone 上的大型电子邮件列表一样。当我开始创建一个类时,我发现了这个问题,该类将根据需要从数据库中排队和出列记录组,以节省内存。我发现当我的 UITableViewController 加载时,它会为典型的前 10 个索引(0 到 9)调用 cellForRowAtIndexPath。当我在模拟器的黑色外表面区域单击鼠标或尝试使用向上手势在 UITableView 上向下滚动时,就会出现问题。此时,为所有剩余索引调用 cellForRowAtIndexPath。即使有 1000 多个单元格,也会发生这种情况。我试图用只有 100 个索引(没什么花哨的)重新创建一个非常简单的项目。它做的事情完全一样。当我滚动时,它会从位置 10 到 99 调用 cellForRowAtIndexPath。
这是 UITableView 的正常行为吗?有谁知道为什么它调用所有行的效率低下?
我会说,一旦这个初始阶段发生,它似乎就开始正常工作了。我对此感到非常困惑,因为它几乎不可能根据需要创建一个用于排队和取消排队记录的类。
这是我的简单得多的示例项目的代码:
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 100;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
printf("RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = %d\n", indexPath.row);
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = @"Example Text";
return cell;
}
控制台:
…… 视图的初始加载 ......
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 0
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 1
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 2
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 3
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 4
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 5
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 6
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 7
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 8
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 9
…… 第一个滚动手势 ......
ootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 11
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 12
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 13
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 14
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 15
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 16
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 17
。 . 我在这里删除了一些以缩短它 . .
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 97
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 98
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 99
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 10
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 11
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 12
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 13
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 14
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 15
RootViewController, -tableView:cellForRowAtIndexPath; // indexPath.row = 16
(gdb)
感谢您的帮助。
埃里克
编辑(后天)--> 今天我想尝试一个真实的设备而不是我的模拟器。我的 iPhone 3GS 没有重现这种奇怪的行为。我相信我机器上的模拟器可能有问题。我打算在时间允许的时候尝试另一个 MAC。
【问题讨论】:
标签: sdk uitableview ios