开发者

UITableView problem displaying contents of multiple sections

I have a table view which displays contents of several possible sections. The problem I am having is that when a section does not appear at the top of the screen and the user has to scroll to it, for some reason the contents of the first section are displayed in the cell label. When the item is selected, it loads the proper data, it just doesn't display correctly in the table view. Here is the cell code:

- (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;
}

When I print a log outside the if block "if (cell == nil)" it appears to show the correct data at all times. However, when I place the log inside this block it does not log the data from the out of view section, and when I scroll down to the data it does nothing - so开发者_StackOverflow it seems like it's not seeing that cell data as nil and assigning it the data from the first section.


A problem I see with this code is that you only set the cell contents when you create a new cell, i.e. inside if (cell == nil) and not when you reuse an already existing cell via dequeueReusableCellWithIdentifier. So, in this case you get a cell with old contents and you don't set the "right" contents. I suggest moving some statements out of the if block:

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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜