开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜