我有一個顯示多個可能部分內容的表格視圖。我遇到的問題是當某個部分沒有出現在屏幕的頂部並且用戶必須滾動到該部分時,出於某種原因,第一部分的內容將顯示在單元格標籤中。當選擇該項目時,它將加載正確的數據,但它只是無法在表格視圖中正確顯示。這裏是單元代碼:顯示多個部分的內容的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