2014-01-14 34 views
0

我有一個UITableView 10個單元格。除了第一個單元格,都是相同的。UITableView:創建後的單元格更新(正確的方法)

約3個單元格一次顯示在屏幕上。每個單元格都有一個標有「Claim」的標籤。根據某些事件,我將某些單元中的「聲明」更改爲「聲明」。

問題是當我滾動單元格時,其他單元格(其「聲明」我沒有更改爲「聲明」)也顯示爲「聲明」。這似乎是隨機的,感覺是由於電池重複使用和不良實施。請查看代碼並幫助我更好地處理此問題。

我的要求是:

  • 顯示器10個細胞外面都是除了第一個相同。
  • 所有相同的單元格都有一個帶有文字「聲明」的按鈕/標籤
  • 當我按下按鈕時,「聲明」應該僅針對該按鈕所在的特定單元格更改爲「聲明」。
  • 當我滾動時,此更改應該保留事件。使用

定製細胞是:

#import "CustomSaloonCell.h" 

@implementation CustomSaloonCell 
@synthesize claimButton; 
@synthesize delegateListener; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 

     self.claimButton=[UIButton buttonWithType:UIButtonTypeSystem]; 
     self.claimButton.frame=CGRectMake(30,140,80, 30); 
     [self.claimButton setTitle:@"claim" forState:UIControlStateNormal]; 
     [self.claimButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal]; 
     self.claimButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:(22)]; 
     [self.claimButton addTarget:self action:@selector(claimButtonPressed) forControlEvents:UIControlEventTouchUpInside]; 
     [self.contentView addSubview:self.claimButton]; 
    } 
    return self; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 



- (void)claimButtonPressed{ 
    [self.delegateListener didClickedClaimButton:self]; 
} 

@end 

細胞生成功能:

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(indexPath.row==0){ 




      CustomHeaderCell *headerCell = [[CustomHeaderCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"header_cell"]; 
     headerCell.selectionStyle = UITableViewCellSelectionStyleNone; 
      return headerCell; 



    } 
    static NSString *cellIdentifier = @"HistoryCell"; 

    // Similar to UITableViewCell, but 
    CustomSaloonCell *cell = (CustomSaloonCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) { 

     cell = [[CustomSaloonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 

     cell.selectionStyle = UITableViewCellSelectionStyleNone; 


    } 


    cell.claimButton.tag = indexPath.row+TAG_OFFSET; 

    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"salon.jpg"]]; 

    cell.delegateListener = self; 

    return cell; 
} 

用於修改標籤的委託方法是:

- (void) claimConfirmedDelegate:(NSInteger)tag{ 



    CustomSaloonCell *selectedCell=(CustomSaloonCell*)[self.claimTableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:(tag-TAG_OFFSET)inSection:0]]; 

    [selectedCell.claimButton setTitle:@"claimed" forState:UIControlStateNormal]; 
} 

回答

1

按鈕的保存狀態(如果您需要,也可以是標題)以單獨的實例可變排列年。 當您按下按鈕並調用委託方法時,在buttonStateArray中添加該單元格的indexpath。 現在檢查當前indexPath中的cellForRowAtIndexPath方法:是否包含buttonStateArray。如果它存在,然後改變你的按鈕狀態,並標題是其他事情,如果你想。 它也會在滾動後工作。

在tableview類的.h文件中聲明NSMutableArray *buttonStateArray;。 在初始化或視圖加載之後分配它。

- (void) claimConfirmedDelegate:(NSInteger)tag{ 

    CustomSaloonCell *selectedCell=(CustomSaloonCell*)[self.claimTableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:(tag-TAG_OFFSET)inSection:0]]; 

    [selectedCell.claimButton setTitle:@"claimed" forState:UIControlStateNormal]; 
    [buttonStateArray addObject:[NSIndexPath indexPathForRow:(tag-TAG_OFFSET)inSection:0]]; 
} 

現在的cellForRowAtIndexPath:方法

for (NSIndexPath *selectedIndex in buttonStateArray){ 
    if([selectedIndex isEqual:indexPath]){ 
     //Change your state of button. 
    } 
} 
+0

你也總是需要設置'cellForRowAtIndexPath'正常的稱號了。總是將標題設置爲某種東西。 – Wain

+0

是的,我完全同意你的看法。但這一切都取決於要求。 – Tirth

+0

如果文本是動態的,那麼你總是需要設置它。如果單元格被重用,那麼您不能假設它包含的文本。 – Wain

相關問題