我在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];
}
產生同樣的結果,無電池再利用。
在此先感謝您提供的任何幫助!
因此,它不會重複使用它,因爲它會模板?看起來有點奇怪,因爲有多少蘋果談論重用。 – pzearfoss 2009-09-24 15:22:09
這取決於。你有多少行?標準表可以同時容納大約8行左右的行,所以它們需要是唯一的對象。但是,如果你說20行,你仍然只有8個左右的對象,他們將在滾動或滾動時被回收。 – jbrennan 2009-09-24 15:46:18