2011-05-23 79 views
0

我有一个显示多个可能部分内容的表格视图。我遇到的问题是当某个部分没有出现在屏幕的顶部并且用户必须滚动到该部分时,出于某种原因,第一部分的内容将显示在单元格标签中。当选择该项目时,它将加载正确的数据,但它只是无法在表格视图中正确显示。这里是单元代码:显示多个部分的内容的UITableView问题

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    Sections *thisSection = [self.sectionsArray objectAtIndex:indexPath.section]; 

    NSArray *sectionMedia = [NSArray arrayWithArray:[thisSection.media allObjects]]; 

    NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"releaseDate" ascending:NO]; 
    NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"sortKey" ascending:YES]; 
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, sortDescriptor2, nil]; 
    sectionMedia = [sectionMedia sortedArrayUsingDescriptors:sortDescriptors]; 

    Media *thisMedia = [sectionMedia objectAtIndex:indexPath.row]; 

    [NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"MMM d, yyyy"]; 
    NSString *articleDisplayDate = [dateFormatter stringFromDate:thisMedia.releaseDate]; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     cell.textLabel.text = thisMedia.title; 
     cell.detailTextLabel.text = articleDisplayDate; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    [dateFormatter release]; 
    [sortDescriptors release]; 
    [sortDescriptor1 release]; 
    [sortDescriptor2 release]; 

    return cell; 
} 

当我打印日志外,如果块“如果(细胞==零)”出现在任何时候,以显示正确的数据。但是,当我将日志放入此块中时,它不会从视图外部分中记录数据,并且当向下滚动到数据时它不会执行任何操作 - 因此它看起来没有将该单元格数据视为零并将其分配给它来自第一部分的数据。

+0

我这个代码中看到的问题是,你只有当你创建一个新的细胞,即内'如果(细胞==零)设置单元格的内容'而不是在通过'dequeueReusableCellWithIdentifier'重用已经存在的单元格时。所以,在这种情况下,你会得到一个包含旧内容的单元格,而不会设置“正确”的内容。 – albertamg 2011-05-23 16:38:49

+0

感谢您的回应 - 那么,您是否建议删除if块? – unclesol 2011-05-23 16:47:03

+0

实际上确实解决了这个问题。当我创建一个表视图时,那个代码是默认的,所以我的假设是它节省了资源。在这种情况下,似乎它对我不利。再次感谢。 – unclesol 2011-05-23 16:48:51

回答

1

我用这段代码看到的一个问题是,当你创建一个新的单元格时,即只在if (cell == nil)之内设置单元格内容,而不是当你通过dequeueReusableCellWithIdentifier重新使用已经存在的单元格时。所以,在这种情况下,你会得到一个包含旧内容的单元格,而不会设置“正确”的内容。我建议把一些语句出来的,如果块:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
} 
cell.textLabel.text = thisMedia.title; 
cell.detailTextLabel.text = articleDisplayDate; 
+1

另一件你可能应该做的事情是,当你获得deQueued单元格时,在填充新值之前,将所有单元格内容重置为空。这样做的原因是,如果您不首先重置这些值,您可能会从该单元格的先前使用中获得剩余值,导致其显示不正确。 – rekle 2011-05-23 18:21:16