0

我有一个 UITableView 显示 UITableView 单元格启用了 UITableViewCellAccessoryCheckmark 选项。我想让用户根据自己的喜好选择多个单元格,完成后,按“完成”按钮。

但是,当按下“完成”按钮时,我需要能够仅将 UITableView 数组中的选定对象添加到单独的数组中。

cell.accessoryType = UITableViewCellAccessoryCheckmark;

回顾一下:用户可以选择任意数量的单元格。完成后,他们按下 UIButton,然后只将选定的单元格添加到另一个数组中。

提前谢谢你的帮助!

更新:

   -(void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [self.setupTable cellForRowAtIndexPath:indexPath];

    [self.selectedCells removeObject:[self.setupFeeds objectAtIndex:indexPath.row]];
    cell.accessoryType = UITableViewCellAccessoryNone;

    [self.setupTable deselectRowAtIndexPath:indexPath animated:YES];
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self.setupTable cellForRowAtIndexPath:indexPath];

    [self.selectedCells addObject:[self.setupFeeds objectAtIndex:indexPath.row]];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;


}
4

1 回答 1

3

您的问题与标题无关,UITableViewCellAccessory因此UITableView标题有些误导。

如果我是你,我会将一个NSMutableArray选定的单元格作为实例变量或属性。

@property (nonatomic, strong) NSMutableArray *selectedCells;

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Maybe some validation here (such as duplicates etc)
    ...
    [self.selectedCells addObject:indexPath];

}

然后按下完成后,您可以检查此属性以查看选择了哪些单元格。

更新:

//Retrieve cell
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

//UITableViewCell has a textLabel property replace "yourLabel" with that if you have used it. Otherwise you can identify the label by subclassing tableviewcell or using tags.
[self.selectedCells addObject:cell.yourLabel.text]; 

//Then if cell is reselected

[self.selectedCells removeObject:cell.yourLabel.text];
于 2013-08-11T21:50:03.783 回答