2011-04-25 45 views
0

我有一個UITableViewController顯示一堆'n'行(都使用標準UITableViewCell類),最後一個有一個圖像(一個小'+'圖標來指示'添加記錄')。UITableViewCell仍然顯示圖像重疊文本後清除

假設以下情形:

  1. 用戶點擊該最後一排,其壓入一個新的視圖控制器到導航堆棧
  2. 然後用戶填寫表單以添加新的記錄,它使用核心數據
  3. 形式然後從導航堆棧中彈出,回到你那第一UITableViewController,我打電話[self.tableView reloadData]以確保顯示新的狀態保存

您現在看到'n + 1'行,最後一行顯示'+'圖像。問題是我遇到了一個錯誤,其中'n'行仍然顯示'+'圖像,只是它與該單元格的文本標籤重疊(我可以理解它向其顯示'+'圖像的錯誤文本的左側 - 我明確地設置imageView.image = nil以避免這種情況 - 但圖像與文本重疊的事實使這一個真的很奇怪)。

下面是我的cellForRowAtIndexPath函數的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *CellIdentifier = @"Cell"; 
    UITableViewCellStyle cellStyle; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];   
    } 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    if ((self.selectedCategory != nil) && (indexPath.row < [self.selectedCategory.subjects count])) { 
     Subject *managedObject = (Subject*)[self.selectedCatSubjects objectAtIndex:indexPath.row]; 
     cell.textLabel.text = managedObject.name; 
     cell.imageView.image = nil; // Needed in case there used to be a "+" icon 
    } else { 
     cell.textLabel.text = @"Add subject..."; 
     [cell.imageView initWithImage:_addIcon]; 
    } 

    return cell; 
} 
+0

我認爲這是與細胞的重用。您可以嘗試單獨使用最後一個單元格的不同單元標識符。 – visakh7 2011-04-25 10:31:31

+0

替換NSString * CellIdentifier = @「Cell」;與NSString * CellIdentifier = [NSString stringWithFormat:@「Cell%d%d」,indexPath.section,indexPath.row]; – Krishnabhadra 2011-04-25 10:33:57

+0

@Krishnabhadra我認爲稍微打破了重用細胞的觀點,不是嗎? @ 7KV7的方法聽起來像少了矯枉過正 – andygeers 2011-04-25 10:39:59

回答

1

我在我的應用程序之一,也有類似的功能,在那裏我有一個加載更多按鈕作爲最後一個單元格。我爲最後一個單元使用了不同的標識符,它爲我工作。

if(indexPath.row==[self.selectedCategory.subjects count]) 
{ 
cellIdentifier = @"PlusCell"; 
} 
2

嘗試使用不同的標識符最後一行:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *CellIdentifier = @"Cell"; 
    UITableViewCellStyle cellStyle; 

    BOOL isFinalRow = (self.selectedCategory == nil) || (indexPath.row >= [self.selectedCategory.subjects count]); 
    if (isFinalRow) { 
     CellIdentifier = @"AddCell"; 
    } 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];   
    } 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    if (!isFinalRow) { 
     Subject *managedObject = (Subject*)[self.selectedCatSubjects objectAtIndex:indexPath.row]; 
     cell.textLabel.text = managedObject.name; 
     cell.imageView.image = nil; // Needed in case there used to be a "+" icon 
    } else { 
     cell.textLabel.text = @"Add subject..."; 
     [cell.imageView initWithImage:_addIcon]; 
    } 

    return cell; 
} 
+0

建議您使用內置的圖像視圖存在問題,因此您正在實施自己的圖像視圖? – andygeers 2011-04-25 10:55:45

+0

我沒有檢查內置的,但這段代碼會給你期望的輸出。 – 2011-04-25 10:58:05

+0

@andygeers你有沒有試過斷點?當第n行被重用時,你會進入你的if條件。 – Krishnabhadra 2011-04-25 11:01:41

1

這對我有效。

取代:

static NSString *CellIdentifier = @"Cell"; 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

有:

NSString *CellIdentifier = [NSString stringWithFormat:@"%d_%d",indexPath.section,indexPath.row]; 
+0

這非常低效,除非你有足夠的理由這麼做 - 你會失去重用單元的所有好處。接受的答案是做這個工作 - 因爲它只是最後一個單元格不同,所以只有最後一個單元格需要不同的標識符 – andygeers 2014-07-15 14:33:06

+0

@andygeers我不認爲它是低效率的,因爲更好地設置每個單元格的唯一標識符而不是檢查單元格如果還有條件。如何有多種不同類型的細胞。 – 2014-07-16 05:59:38