开发者

iPhone: How to allow multiple selection in tabelview for a custom cell?

开发者 https://www.devze.com 2023-03-26 13:00 出处:网络
How can I adapt this to be able to make multiple selections? and get the selected ones - (id)initWithCellIdentifier:(NSString *)cellID {

How can I adapt this to be able to make multiple selections? and get the selected ones

- (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];            
}

And the selected method is:

- (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 AP开发者_运维技巧P_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]];
        }
    }
  }
}


For multiple selection, setup an NSMutableArray ivar (selectedIndexPaths in this case) to hold the items that are selected. In didSelectRowAtIndexPath add or remove indexPaths to this array.

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

Use selectedIndexPaths later to do whatever you wish! Cheers!

-Akshay

0

精彩评论

暂无评论...
验证码 换一张
取 消