2014-08-28 52 views
0

我有一個顯示一些信息的徽章的自定義單元格。我只想爲某些單元格設置徽章圖像和文本(取決於我從Web服務響應中獲得的某些標記)。但是當我重複使用這個單元格時,徽章及其文本會變得重複。如何解決。UITableView中的單元子視圖重複問題

徽章及其文本是自定義單元格的一部分,並未通過代碼添加爲子視圖。

- (void) setBadgeText:(VBMerchantDealCell *)cell withObject:(VBDeals *)deals{ 


    if ([deals.dealType intValue] == PUNCHCARDDEAL) { 
     if ([deals.punchStatus intValue] == UNIV_INDEX_ONE && ![VBUtility isNullValue:deals.punchSpecialMessage]) { 
      [cell.lblBadgeLabel setText:deals.punchSpecialMessage]; 
     }else { 
      [cell.lblBadgeLabel setText:[self getBadgeTextForPCD:deals]]; 

     } 
     return; 
    } 
} 
+0

您可以發佈您的cellForRowAtIndexPath代碼?這聽起來像你沒有正確地重複使用單元格。 – Veeru 2014-08-28 08:05:40

回答

0

嘗試以下操作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     // Cell configurations 
     [self setBadgeText:indexPath :deals]; 
     return cell; 
    } 
- (void) setBadgeText:(NSIndexPath *)indexPath withObject:(VBDeals *)deals 
    { 
    [self.tableView beginUpdates]; 
    YourTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cellIdentifier" forIndexPath:indexPath];] 
    if ([deals.dealType intValue] == PUNCHCARDDEAL) 
     { 
     if ([deals.punchStatus intValue] == UNIV_INDEX_ONE && ![VBUtility isNullValue:deals.punchSpecialMessage]) 
      [cell.lblBadgeLabel setText:deals.punchSpecialMessage]; 
     else 
      [cell.lblBadgeLabel setText:[self getBadgeTextForPCD:deals]]; 
     } 
    [self.tableView endUpdates]; 
    } 
+0

我試過這種方法。當細胞不再可見時,使細胞爲零。這是正確的方法。這會不會造成任何問題 - (空)的tableView:(UITableView的*)的tableView didEndDisplayingCell:(*的UITableViewCell)單元forRowAtIndexPath:(NSIndexPath *)indexPath { 如果([tableView.indexPathsForVisibleRows indexOfObject:indexPath] == NSNotFound){ VBMerchantDealCell * cell =(VBMerchantDealCell *)[tableView cellForRowAtIndexPath:indexPath]; cell = nil; } } – 2014-08-28 08:13:14

+0

有什麼獨立方法的目的。你有沒有嘗試過cellForRowAtIndexPath本身? – 2014-08-28 09:24:18

+0

表格視圖單元格包含一個切換標誌,該切換標誌每3秒切換一次。在此切換過程中,徽章圖像和徽章中顯示的文本將發生變化。這些都是用獨立的方法處理的。 目前我無法重用單元格,因爲所有單元格都不包含相同的UI元素。所以我讓didEndDisplaying方法中的單元格爲零。任何人都可以告訴這會造成任何性能問題。 – 2014-08-29 06:10:43