2015-02-23 37 views
0

我爲我的UITableViewCell創建了一個CheckBoxView控件。我面臨的問題是,一旦我勾選頂行之一併滾動,底部行上就會顯示相同的複選標記。這是因爲dequeueresuable行功能,我想知道如何解決它。這是實施。自定義CheckBoxView爲UITableView複製複選標記

CheckBoxView.m:

-(instancetype) initWithCoder:(NSCoder *)aDecoder { 

    self = [super initWithCoder:aDecoder]; 
    [self setup]; 
    [self registerGesturesRecognizers]; 
    return self; 
} 

-(void) registerGesturesRecognizers { 

    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(checkBoxTapped:)]; 

    [self addGestureRecognizer:tapGestureRecognizer]; 
} 

-(void) checkBoxTapped:(UITapGestureRecognizer *) recognizer { 

    if(self.checkBoxViewSelectionChanged) { 
     if(!self.isChecked) { 
      self.checkBoxViewSelectionChanged(self,self.isChecked); 
      self.isChecked = YES; 
     } 
     else { 
      self.checkBoxViewSelectionChanged(self,self.isChecked); 

     } 
    } 
} 

-(void) check { 

    [_checkBoxImageView setImage:[UIImage imageNamed:@"small-check"]]; 

} 

-(void) uncheck { 

    _checkBoxImageView.image = nil; 
} 

-(void) setup { 

    self.userInteractionEnabled = YES; 
    self.layer.borderWidth = 0.5f; 
    self.layer.borderColor = [UIColor lightGrayColor].CGColor; 

    _checkBoxImageView = [[UIImageView alloc] initWithFrame:CGRectMake(1, 0, 23, 23)]; 
    [self addSubview:_checkBoxImageView]; 
} 

這裏是的cellForRowAtIndexPath方法:

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

    SamplesTableViewCell *cell = (SamplesTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"SamplesTableViewCell" forIndexPath:indexPath]; 

    Item *sample = [_samples objectAtIndex:[indexPath row]]; 

    cell.productNameLabel.text = sample.product.name; 
    cell.productColorLabel.text = sample.productColor.name; 
    [cell.productImageView setImage:sample.productColor.image]; 

    cell.checkboxView.checkBoxViewSelectionChanged = ^(CheckBoxView *checkBoxView, BOOL isChecked) { 

     if(!isChecked) { 
      [checkBoxView check]; 
      checkBoxView.isChecked = YES; 
     } 
     else { 
      [checkBoxView uncheck]; 
      checkBoxView.isChecked = NO; 
     } 

    }; 

    return cell; 
} 

的CheckBoxView實際上是對故事板原型細胞,其類設置爲CheckBoxView一個UIView所以它不是在cellForRowAtIndexPath事件中動態創建的。當我運行上面的代碼並勾選最上面的行並滾動時,下面的行上出現相同的複選標記。

UPDATE:

這是我更新的代碼,但它仍然覈查,並在底部取消選中的行。

cell.checkboxView.checkBoxViewSelectionChanged = ^{ 

     if(!sample.isSelected) { 

      [sample setSelected:YES]; 
      [cell.checkboxView check]; 
     } 
     else 
     { 
      [sample setSelected:NO]; 
      [cell.checkboxView uncheck]; 
     } 


    }; 

回答

3

UIView應該來維持你的複選框isChecked狀態。正如您所指出的那樣,由於電池重複使用,所有其他電池將被檢查,因爲正在使用相同的UIView

支持數據的模型對象需要維護狀態或控制器。

例如:

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

    SamplesTableViewCell *cell = (SamplesTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"SamplesTableViewCell" forIndexPath:indexPath]; 

    // Some code... 

    if ([self.myDatasource[indexPath.row] isChecked]) { 
     cell.checkBoxView.isChecked = YES; 
    } 

    return cell; 
} 

只是一些粗糙的僞代碼,但希望你的想法。實現此狀態的潛在方式是以模型對象的形式進行,或者在您的UIViewController中有一個NSArray,其中包含代表UITableView中每行的NSDictionary,其中每個鍵值對都爲您的UITableViewCell保持特定狀態。