iPhone: Как разрешить множественный выбор в tabelview для пользовательской ячейки? - PullRequest
4 голосов
/ 08 августа 2011

Как я могу адаптировать это, чтобы иметь возможность сделать несколько выборов? и получите выбранные

- (id)initWithCellIdentifier:(NSString *)cellID {
if ((self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID])) {

    UITableViewCell *cell=self; 
            UIImage *cry = [UIImage APP_CRYSTAL_SELECT];
    self.leftImage = [[[UIImageView alloc] initWithImage:cry] autorelease] ;
            [self.contentView addSubview:leftImage];            
}

И выбранный метод:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
      if(selected)
      {
       NSArray *subviews=[self.contentView subviews];
        for(UIView* view in subviews){
          if([view isEqual:self.leftImage]){
             [self.leftImage setHighlightedImage:[UIImage APP_CRYSTAL_SELECTED]];
        }
    }
}
else
{       
    NSArray *subviews=[self.contentView subviews];
    for(UIView* view in subviews){
        if([view isEqual:self.leftImage]){
            [self.leftImage setHighlightedImage:[UIImage APP_CRYSTAL_SELECT]];
        }
    }
  }
}

1 Ответ

16 голосов
/ 09 августа 2011

Для множественного выбора настройте ивар NSMutableArray (в этом случае selectedIndexPaths), чтобы хранить выбранные элементы. В didSelectRowAtIndexPath добавьте или удалите indexPaths к этому массиву.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{   
        if(![self.selectedIndexPaths containsObject:indexPath])
            [self.selectedIndexPaths addObject:indexPath];
        else
            [self.selectedIndexPaths removeObject:indexPath];
}

Используйте selectedIndexPaths позже, чтобы делать что угодно! Ура! * * 1005

-Akshay

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...