2011-02-16 73 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子句来处理重用单元。