我一直在处理有关UITableView和cellForRowAtIndexPath的问题。UITableViewCell cellForRowAtIndexPath第一次滚动时调用大量的行
我想让用户拥有非常大量的行,就像iPhone上的大量电子邮件列表一样。当我开始创建一个类时,我会发现这个问题,该类会根据需要对数据库中的记录组进行排队和排队,以节省内存。我发现当我的UITableViewController加载时,它会为典型的前10个索引(0到9)调用cellForRowAtIndexPath。当我在模拟器的黑色外表面区域上单击鼠标时出现问题,或尝试使用向上的手势向下滚动UITableView。此时,cellForRowAtIndexPath被所有其余索引调用。即使有1000多个单元,也会发生这种情况。我试图重新创建一个只有100个索引的非常简单的项目(没有什么奇特的)。它完全相同的事情..只要我滚动,它调用cellForRowAtIndexPath从位置10到99.
这是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。
感谢你这么多的这个答案。我被困在完全相同的probme上,并使用了Cinch。你为我节省了很多浪费时间。 – CodeFlakes 2011-01-10 11:50:44
你是我的英雄!我从来没有想过这一点。非常感谢。 – Larry 2011-12-09 11:13:09
@geekinit请选择您的答案 – 2011-12-13 19:46:36