2014-08-28 26 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