2011-02-16 23 views
1


這是我的cellForRowAtIndexPath表格視圖中的重複單元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Maledetta"]; 
if (cell == nil) { 
    UIViewController *c; 
    if (!IS_IPAD) c = [[UIViewController alloc] initWithNibName:@"NewsRow" bundle:nil]; 
    else c = [[UIViewController alloc] initWithNibName:@"NewsRow_ipad" bundle:nil]; 
    cell = (NewsRowController*)c.view; 

    if ([titleArray count] > 0) { 
     [(NewsRowController*)cell setCellDataWithName:[titleArray objectAtIndex:indexPath.row] 
               andDate:[descArray objectAtIndex:indexPath.row] 
                day:[dayArray objectAtIndex:indexPath.row] 
               month:[monthArray objectAtIndex:indexPath.row]]; 
    } 
    [c release]; 
} 
return cell; 
} 

爲什麼它顯示我只有4行,之後,重複第一次,其他時間第四次,直到十?

+-----------------------+ 
| A 
+-----------------------+ 
| B 
+-----------------------+ 
| C 
+-----------------------+ 
| D 
+-----------------------+ 
| A (repeated) 
+-----------------------+ 
| B (repeated) 
+-----------------------+ 
| C (repeated) 
+-----------------------+ 
| D (repeated) 
+-----------------------+ 
| A (repeated) 
+-----------------------+ 
| B (repeated) 
+-----------------------+ 

啊,[titleArray count]等於10kCustomCellID是正確的。

謝謝。
A

+0

我會建議逐步使用調試器並逐個檢查您的值。如果需要,可以使用「po對象」打印出對象的原始細節。 – clifgriffin 2011-02-16 20:18:16

回答

5

如果在表格的單元隊列中找不到該單元格,則只填充該單元格。如果找到了,則不會用indexPath.row的該值的內容覆蓋它。

試試這個:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Maledetta"]; 
if (cell == nil) { 
    UIViewController *c; 
    if (!IS_IPAD) c = [[UIViewController alloc] initWithNibName:@"NewsRow" bundle:nil]; 
    else c = [[UIViewController alloc] initWithNibName:@"NewsRow_ipad" bundle:nil]; 
    cell = (NewsRowController*)c.view; 
    [c release]; 

} 

if ([titleArray count] > 0) { 
     [(NewsRowController*)cell setCellDataWithName:[titleArray objectAtIndex:indexPath.row] 
               andDate:[descArray objectAtIndex:indexPath.row] 
                day:[dayArray objectAtIndex:indexPath.row] 
               month:[monthArray objectAtIndex:indexPath.row]]; 
    } 
    return cell; 
} 

此外,[titleArray計數]的檢查可能是多餘的。你正在用這個來給這個表的部分單元格的數量,對吧?如果那是零,它甚至不會到達這裏。

+0

很棒!你想喝啤酒嗎? ! – elp 2011-02-16 20:22:49

0

當您調用[tableView dequeueReusableCellWithIdentifier:@"Maledetta"]時,表視圖會在其緩存中查找不在屏幕上的單元格。它找到你的細胞並使用它們。在你的單元上實現-prepareForReuse,然後在你的-cellForRowAtIndexPath:實現中添加一個else子句來處理重用單元。

相關問題