检测选择了哪些单元格

检测选择了哪些单元格

本文介绍了UITableViewCellAccessoryCheckmark多个选择/检测选择了哪些单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableView,其中显示了启用了UITableViewCellAccessoryCheckmark选项的UITableView单元格。我想让用户选择自己喜欢的多个单元格,并在完成后按完成按钮。

I have a UITableView that shows UITableView cells with the UITableViewCellAccessoryCheckmark option enabled. I would like to let the user select multiple cells to their liking and when their done, press a "Done" button.

但是,当按下完成按钮时,我只需要将UITableView数组中的选定对象添加到一个单独的数组中即可。

However, when the "Done" button is pressed, I need to be able to add ONLY the selected object from the UITableView array into a separate array.

cell.accessoryType = UITableViewCellAccessoryCheckmark;

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

To recap: User can select as many cells as they want. When done, they press a UIButton which then adds ONLY the selected cells to another array.

谢谢您的帮助!

更新:

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


}


推荐答案

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

Your question does not relate to UITableViewCellAccessory or UITableView so the title is somewhat misleading.

如果我是我,我会保存所选单元格的 NSMutableArray 作为实例变量或属性。

If I were you I would hold an NSMutableArray of selected cells as an instance variable or property.

@property (nonatomic, strong) NSMutableArray *selectedCells;

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

}

然后在按下完成后,您可以检查此属性以

And then when done is pressed you may check this property to see which cells are selected.

更新:

//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];

这篇关于UITableViewCellAccessoryCheckmark多个选择/检测选择了哪些单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 09:15