2009-09-24 105 views
1

早上好。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]; 
    } 

產生同樣的結果,無電池再利用。

在此先感謝您提供的任何幫助!

回答

2

表格通常會在需要重新使用它們之前創建並使用一些單元格(通常大約5個)。這當然取決於它們的大小以及在任何給定時間出現在屏幕上的數量。沒什麼好擔心的。

+0

因此,它不會重複使用它,因爲它會模板?看起來有點奇怪,因爲有多少蘋果談論重用。 – pzearfoss 2009-09-24 15:22:09

+0

這取決於。你有多少行?標準表可以同時容納大約8行左右的行,所以它們需要是唯一的對象。但是,如果你說20行,你仍然只有8個左右的對象,他們將在滾動或滾動時被回收。 – jbrennan 2009-09-24 15:46:18

-1

嘗試使CellIdentifier爲靜態,例如,

static NSString *CellIdentifier = @"Cell"; 
+0

已經是靜態的。 。 。 – pzearfoss 2009-09-24 15:21:30

+0

在問題的第一個版本中不是靜態的.... – Thys 2009-09-24 22:07:53

2

我不確定每次打開應用程序時你的意思是什麼?

使用對象分配工具運行代碼,並在滾動tableview時查看對象分配的圖形。如果對象分配即使在幾次滾動後也會線性增加。那麼你有一個問題。

+0

偉大的建議,謝謝! – pzearfoss 2009-09-24 15:26:05