IOS UITableView 多选,全选删除,拖动,置顶的实现

附上效果图






第一步 
要想看到tableview的多选模式,这两句代码必不可少
1.设置为编辑模式

    [self.tableView setEditing:YES animated:NO];数组

2.设置编辑样式为多选(两个属性加起来就是这么神奇)post


-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPathspa

{orm

    return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;事件

}ci


0 处理全选 //实现点击全选按钮,实现所有选中,再点击删除,删除全部选中的cellrem

//暂时没作反选字符串

//全选按钮点击事件get

-(void)allChoiceBtnClick:(UIButton *)btnstring

{

    btn.selected = ! btn.selected;

    if (btn.selected) {

        //将可见的设为全选状态

        for (NSIndexPath *cellIndex in [self.tableView indexPathsForVisibleRows])

        {

            KeChooseManagerCell * otherCell = (KeChooseManagerCell *)[self.tableViewcellForRowAtIndexPath:cellIndex];

            otherCell.selected = YES;

            self.isAllSelected = YES;

        

            self.indexArr = [NSMutableArray new];

            

            //遍历数组建立含有sectionrowNSIndexPath,而后将它传给tableview,用来删除所有选中的cell

            for (int i = 0; i < self.stockList.count; i++) {

                

                

                NSIndexPath * realIndex = [NSIndexPath indexPathForRow:i inSection:0];

                

                [self.indexArr addObject:realIndex];


            }

            


        }

    }else{

        

        for (NSIndexPath *cellIndex in [self.tableView indexPathsForVisibleRows])

        {

            KeChooseManagerCell * otherCell = (KeChooseManagerCell *)[self.tableViewcellForRowAtIndexPath:cellIndex];

            otherCell.selected = NO;

            self.isAllSelected = NO;

        }

    }

    

}

//删除点击处理

-(void)deleteBtnClick:(UIButton *)btn

{

    if (self.isAllSelected) {//全选

        

        [self.stockList removeAllObjects];

        [self.tableView beginUpdates];

        

        KELog(@"allKeys %@",[deleteDic allKeys]);

        KELog(@"stockcount  %d",self.stockList.count);

        KELog(@"stock %@",self.stockList);

        

        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithArray:self.indexArrwithRowAnimation:UITableViewRowAnimationFade];

        

        KELog(@"allValues %@",[deleteDic allValues]);

        

        //    [deleteDic removeAllObjects];

        

        [self.tableView endUpdates];

        [self.tableView reloadData];

        

        newDataList = self.stockList;

        

    }else

    {//非全选

        //动态绑定(运行时有效<不是编译时起做用>)

        __block NSMutableArray * temp = [NSMutableArray new];

        

        __block typeof(self) weakSelf = self;

        

        [indexArray enumerateObjectsUsingBlock:^(ChooseStockInfo * obj, NSUInteger idx, BOOL * _Nonnull stop) {

            

            NSIndexPath * indexPath = objc_getAssociatedObject(obj, &kKeyForindexAndModel);

            

            [temp addObject:indexPath];

            

            [weakSelf.stockList removeObject:obj];

            

            

        }];

        

        [self.tableView beginUpdates];

        

        KELog(@"allKeys %@",[deleteDic allKeys]);

        KELog(@"stockcount  %d",self.stockList.count);

        KELog(@"stock %@",self.stockList);

        

        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithArray:temp] withRowAnimation:UITableViewRowAnimationFade];

        

        KELog(@"allValues %@",[deleteDic allValues]);

        

        //    [deleteDic removeAllObjects];

        

        [self.tableView endUpdates];

        [self.tableView reloadData];

        

        newDataList = self.stockList;

}



//选中

//选中

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    ChooseStockInfo * model = [self.stockList objectAtIndex:indexPath.row];

    

    [indexArray addObject:model];


    //绑定modelindexPath

    objc_setAssociatedObject(model, &kKeyForindexAndModel, indexPath, OBJC_ASSOCIATION_RETAIN);    

    KELog(@"didSelect %@",deleteDic);


}


//取消选中

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

{

    [deleteDicremoveObjectForKey:[self.stockListobjectAtIndex:indexPath.row]];

    

    [indexArray removeObject:indexPath];

    

    KELog(@"Deselect %@",deleteDic);

    

}


1.拖动

//设置可拖动

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {

    return YES;

}

//拖动以后处理的业务逻辑

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    
    //更新沙盒里的自选股字符串数组
    NSArray * array = (NSArray *)[[SandBox shareInstance] getCoreInfoFromSandBoxWithKey:stockListSandBox];
    stockStrLists = [NSMutableArray arrayWithArray:array];
    
    //向上拖动
    if (fromIndexPath.row > toIndexPath.row) {
        
        [self.stockList insertObject:[self.stockList objectAtIndex:fromIndexPath.row] atIndex:toIndexPath.row];

        [self.stockList removeObjectAtIndex:fromIndexPath.row+1];
        
        //到哪一行
        [tableView reloadData];
        
        //string
        [stockStrLists insertObject:[stockStrLists objectAtIndex:fromIndexPath.row] atIndex:toIndexPath.row];
        [stockStrLists removeObjectAtIndex:fromIndexPath.row+1];
        
        newDataList = self.stockList;
        
        [[SandBox shareInstance] saveCoreInfoToSandBox:stockStrLists withKey:stockListSandBox];
        
        self.changed = YES;
        
        [[NSNotificationCenter defaultCenter] postNotificationName:kStockSortChangedNotification object:@{@"stockList":newDataList,@"operationType":@(Sort),@"stockStrList":stockStrLists}];
        
        
    }else//向下拖动
    {
        [self.stockList insertObject:[self.stockList objectAtIndex:fromIndexPath.row] atIndex:toIndexPath.row];
        
        [self.stockList removeObjectAtIndex:fromIndexPath.row - 1];
        
        [tableView reloadData];
        
        [stockStrLists insertObject:[stockStrLists objectAtIndex:fromIndexPath.row] atIndex:toIndexPath.row];
        
        [stockStrLists removeObjectAtIndex:fromIndexPath.row - 1];
        
        newDataList = self.stockList;
        
        [[SandBox shareInstance] saveCoreInfoToSandBox:stockStrLists withKey:stockListSandBox];
        
        self.changed = YES;
        
        [[NSNotificationCenter defaultCenter] postNotificationName:kStockSortChangedNotification object:@{@"stockList":newDataList,@"opertationType":@(Sort),@"stockStrList":stockStrLists}];
        
    }

    
}



2.设置置顶

[cell setTopBlcok:^(){

        KELog(@"--- %ld ----",(long)indexPath.row);

        //插入到第一个

        [self.stockListinsertObject:[self.stockListobjectAtIndex:indexPath.row]atIndex:0];

        ChooseStockInfo * aa = (ChooseStockInfo *)[self.stockListobjectAtIndex:indexPath.row];

        KELog(@"dataList  %@",aa.stockName);

        //删除挪到下一行的元素

        [self.stockListremoveObjectAtIndex:indexPath.row +1];

        

        [tableView reloadData];

        

        newDataList =self.stockList;

        

        self.changed =YES;

        KELog(@"newDataListTop %@",newDataList);

        

        KELog(@"stockStrLists  begin %@",stockStrLists);

        NSArray * array = (NSArray *)[[SandBoxshareInstancegetCoreInfoFromSandBoxWithKey:stockListSandBox];

        stockStrLists = [NSMutableArrayarrayWithArray:array];

        [stockStrListsinsertObject:[stockStrListsobjectAtIndex:indexPath.row]atIndex:0];

        [stockStrLists removeObjectAtIndex:indexPath.row + 1];

        

        KELog(@"stockStrLists end %@ indexrow%ld",stockStrLists,(long)indexPath.row);

        

        [[SandBox shareInstancesaveCoreInfoToSandBox:stockStrListswithKey:stockListSandBox];

        

        [[NSNotificationCenterdefaultCenterpostNotificationName:kStockSortChangedNotificationobject:@{@"stockList":newDataList,@"operationType":@(Sort),@"stockStrList":stockStrLists}];

        

        KELog(@"newData %@",newDataList);

    }];


//注释没有整理,可是上述代码的功能都是OK的
记在成长的路上