UITableView不重用单元格?
问题描述:
我在3.1应用程序中遇到了表视图的小危机。我有一个表视图,其中一些复杂的tableViewCells是从一个笔尖加载的,而且它们不会被重用。感觉这可能与从笔尖加载它们有关,我决定尝试一个简单的测试项目,以查看单元格是否正在被重用,除了基本的苹果模板代码。我开始与导航基于模板,这里是对的cellForRowAtIndexPath代码:我有一个断点设置在下面一行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];
return cell;
}
,而且越来越要求每通。
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]
设置在构造函数中reuseIdentifier这样:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease];
}
产生同样的结果,无电池再利用。
在此先感谢您提供的任何帮助!
答
表格通常会在需要重新使用它们之前创建并使用一些单元格(通常大约5个)。这当然取决于它们的大小以及在任何给定时间出现在屏幕上的数量。没什么好担心的。
答
我不确定每次打开应用程序时你的意思是什么?
使用对象分配工具运行代码,并在滚动tableview时查看对象分配的图形。如果对象分配即使在几次滚动后也会线性增加。那么你有一个问题。
+0
伟大的建议,谢谢! – pzearfoss 2009-09-24 15:26:05
因此,它不会重复使用它,因为它会模板?看起来有点奇怪,因为有多少苹果谈论重用。 – pzearfoss 2009-09-24 15:22:09
这取决于。你有多少行?标准表可以同时容纳大约8行左右的行,所以它们需要是唯一的对象。但是,如果你说20行,你仍然只有8个左右的对象,他们将在滚动或滚动时被回收。 – jbrennan 2009-09-24 15:46:18