通过委托选择Tableview行是选择多行而不是一行

时间:2014-10-23 18:27:19

标签: ios objective-c uitableview

因此,当我点击自定义单元格上的按钮时,它应取消选择并选择图像(更改代表已选中和未选中的图像)。但是如果我有一个更长的表视图,我注意到表视图的每第五个单元格被选中或取消选择。例如,如果我按下第二个单元格上的按钮,我的第七个和第12个单元格也会被选中/取消选择。我猜这是与dequeuereusablecellwithidentifier有关,但不知道如何解决它。 所以我有一个自定义单元类(称为CounterOfferAddGameViewCell),其协议具有一个必需的方法。 .h文件

@class CounterOfferAddGameViewCell;
@protocol CellDelegate <NSObject>

@required
-(void)tableViewCell:(CounterOfferAddGameViewCell *)cell didFireAddGameButtonForSender:(id)sender;
@end
....
@property (strong, nonatomic) IBOutlet UIButton *addGameButton;
- (IBAction)addGameButtonClick:(UIButton *)sender;
@property (weak, nonatomic) id<CellDelegate>delegate;
@property (assign, nonatomic) NSInteger cellIndex;

和实施:

@implementation CounterOfferAddGameViewCell
- (void)awakeFromNib {
    // Initialization code
}

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

    // Configure the view for the selected state
}


- (IBAction)addGameButtonClick:(UIButton *)sender {
    [self.delegate tableViewCell:self didFireAddGameButtonForSender:sender];
}
@end

这是我在表格视图中的代码:

-(void)tableViewCell:(CounterOfferAddGameViewCell *)cell didFireAddGameButtonForSender:(id)sender
{
    if([[selectedGames objectAtIndex:cell.cellIndex] boolValue]){
        [cell.addGameButton setImage:[UIImage imageNamed:@"addButtonUnselected"] forState:UIControlStateNormal];
        [selectedGames replaceObjectAtIndex:cell.cellIndex withObject:@NO];
    }
    else{
    [cell.addGameButton setImage:[UIImage imageNamed:@"addButtonIcon"] forState:UIControlStateNormal];
        [selectedGames replaceObjectAtIndex:cell.cellIndex withObject:@YES];
    }

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *simpleTableIdentifier = @"CounterOfferAddGameViewCell";

    CounterOfferAddGameViewCell *thisCell = (CounterOfferAddGameViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    NSLog(@"%i",thisCell.cellIndex);
    if (thisCell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CounterOfferAddGameViewCell" owner:self options:nil];
        thisCell = [nib objectAtIndex:0];
    }
    thisCell.delegate = self;
    thisCell.cellIndex = indexPath.row;
...

selectedGames只是一个包含布尔值的数组:

for(int i=0;i<[gameCoreModelArray count];i++){
    [selectedGames insertObject:@NO atIndex:i];
}

由于

1 个答案:

答案 0 :(得分:1)

看起来您正在重复使用之前选择的单元格而不将其状态重置为未选中状态。您应该可以通过调用cellForRowAtIndexPath:并从setSelected:NO删除图片,在addGameButton方法的代码中解决此问题:

CounterOfferAddGameViewCell *thisCell = (CounterOfferAddGameViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

NSLog(@"%i",thisCell.cellIndex);
if (thisCell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CounterOfferAddGameViewCell" owner:self options:nil];
    thisCell = [nib objectAtIndex:0];
}
thisCell.cellIndex = indexPath.row;
if([[selectedGames objectAtIndex:thisCell.cellIndex] boolValue]){
    [thisCell.addGameButton setImage:[UIImage imageNamed:@"addButtonIcon"] forState:UIControlStateNormal];
} else {
    [thisCell.addGameButton setImage:[UIImage imageNamed:@"addButtonUnselected"] forState:UIControlStateNormal];
}