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 に答える