iPhone:如何在tabelview中为自定义单元格进行多项选择?

Spr*_*ing 4 iphone ios4 ios

我如何调整它以便能够进行多项选择?并获得所选的

- (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];            
}
Run Code Online (Sandbox Code Playgroud)

选择的方法是:

- (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]];
        }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Aks*_*hay 16

对于多重选择,请设置NSMutableArray ivar(在本例中为selectedIndexPaths)以保存所选项目.在didSelectRowAtIndexPathindex /中添加或删除此数组.

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

稍后使用selectedIndexPaths做任何你想做的事情!干杯!

-Akshay