我有一个显示多个可能部分内容的表格视图。我遇到的问题是当某个部分没有出现在屏幕的顶部并且用户必须滚动到该部分时,出于某种原因,第一部分的内容将显示在单元格标签中。当选择该项目时,它将加载正确的数据,但它只是无法在表格视图中正确显示。这里是单元代码:显示多个部分的内容的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;
}
当我打印日志外,如果块“如果(细胞==零)”出现在任何时候,以显示正确的数据。但是,当我将日志放入此块中时,它不会从视图外部分中记录数据,并且当向下滚动到数据时它不会执行任何操作 - 因此它看起来没有将该单元格数据视为零并将其分配给它来自第一部分的数据。
我这个代码中看到的问题是,你只有当你创建一个新的细胞,即内'如果(细胞==零)设置单元格的内容'而不是在通过'dequeueReusableCellWithIdentifier'重用已经存在的单元格时。所以,在这种情况下,你会得到一个包含旧内容的单元格,而不会设置“正确”的内容。 – albertamg 2011-05-23 16:38:49
感谢您的回应 - 那么,您是否建议删除if块? – unclesol 2011-05-23 16:47:03
实际上确实解决了这个问题。当我创建一个表视图时,那个代码是默认的,所以我的假设是它节省了资源。在这种情况下,似乎它对我不利。再次感谢。 – unclesol 2011-05-23 16:48:51