0

我的问题是,当我选择一个单元格时,它也会自动选择其他单元格。此外,当我取消选择该选定单元格时,自动选择的单元格也会被取消选择。任何帮助深表感谢。谢谢你。

我的代码是:

#pragma mark
#pragma mark UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
     return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [cellDataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"customCellSearchBySpeciality";
    SpecialityCustomCell *cell = (SpecialityCustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SearchBySpecialityViewController" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    } 

    cell.cellData.text = [cellDataArray objectAtIndex:indexPath.row];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    SpecialityCustomCell *selectedCell = (SpecialityCustomCell *)[tableView cellForRowAtIndexPath:indexPath];

    if (selectedCell.selectedCellImageView.image) {
        selectedCell.selectedCellImageView.image = nil;
    }
    else{
        [selectedCell.selectedCellImageView setImage:[UIImage imageNamed:@"image"]];
    } 
}
4

1 回答 1

1

我认为您需要维护您在每一行上添加的单元格的实例,然后对单元格图像视图的特定对象进行选择和取消选择,如下所示,

 NSMutableArray *cellArray;

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"customCellSearchBySpeciality";
    SpecialityCustomCell *cell = (SpecialityCustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    [cellArray addObject:cell];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SearchBySpecialityViewController" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.cellData.text = [cellDataArray objectAtIndex:indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    for(int cc=0;cc<[cellArray count];cc++){
    if(indexPath.row==cc){
    if ([cellArray objectAtIndex:cc].selectedCellImageView.image) {
        [cellArray objectAtIndex:cc].selectedCellImageView.image = nil;
    }
    else{
        [[cellArray objectAtIndex:cc].selectedCellImageView setImage:[UIImage  imageNamed:@"image"]];
    }
  }
}
于 2013-02-15T06:38:41.697 回答