如何在UITableView中使用UITableViewCellAccessory选中标记进行多选

时间:2016-02-23 16:49:49

标签: ios objective-c uitableview

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    [self.tableView setAllowsMultipleSelection:YES];
    [self.tableView setAllowsMultipleSelectionDuringEditing:YES];
    if([self.tableView endEditing:YES])
    { 
       self.detailViewController = [[DetailViewController alloc] init]; 
       NSArray *personels = [[PersonelList sharedList] allPersonel]; 
       Personnel *selectedPersonel = personels[indexPath.row]; 
       _detailViewController.personel = selectedPersonel; 
      [self.navigationController pushViewController:_detailViewController animated:YES];
    }

    }  
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  {
         return UITableViewCellAccessoryCheckmark;
  }

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellSelectionStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
 { 
    if (editingStyle == UITableViewCellAccessoryCheckmark) {

                NSArray *selectedRows = [tableView indexPathsForSelectedRows];
                NSMutableArray *mySelectedObjects = [NSMutableArray array];
                for (NSIndexPath *indexPath in selectedRows) 
                {
                    NSArray *personels = [[PersonelList sharedList] allPersonel];
                    Personnel *selectedPersonel = personels[indexPath.row];
                    [mySelectedObjects addObject:selectedPersonel];
                    NSLog(@"selection objects: %@",mySelectedObjects);
                }
                [self saveSelects];
                [self.tableView endEditing:YES];

           }

 }

我想在表格视图编辑模式中进行多选并保存  数组中选定的单元格。这是我的编辑模式代码,但是当我点击底部的复选标记时,我的视图会在详细视图中导航。我怎么能做这个工作 ?

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。我在表视图单元格中创建UIButton,其中包含表视图内容大小,在详细视图中添加定义块,并在表视图中实现块,选择了行方法。

@interface DetailViewController ()
@property (copy, nonatomic) void (^actionBlockDetail)(void);
//
@implementation EmployeeViewController
 cell.actionBlock = ^{
        PersonnelCell *strongCell = weakCell;

        self.favP = [[NSMutableArray alloc]init];

        if (searchController.active) {
            strongCell.backgroundColor = [UIColor lightGrayColor];
            Personnel *selectedPersonel = self.resultPersonnel[indexPath.row];
            self.detailViewController.personel = selectedPersonel;
            indexP = indexPath;

            if (!self.splitViewController) {
                [self.navigationController pushViewController:self.detailViewController
                                                     animated:YES];
                double delayInSeconds = 0.5;
                dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
                dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                    strongCell.backgroundColor = [UIColor whiteColor];
                });
            }