本次讲解如何在移动端做Tableview的全选,返选,删除的功能;利用Tableview官方自带的方法,直观简洁好使。

ios表格删除 苹果手机如何删除表格_数组

一:功能

1.默认右上角“编辑”
2.点击编辑,显示下方View样式“全选/返选”,“删除”功能,并编辑文本变为“取消”,点击取消就退出了。
3.列表的全选/返选/取消某个列表的选择等

二:思路分析
1.布局样式/代理/点击按钮方法实现
2.定义数据层。获取到总数据列表,选择了某个列表就赋值给新的待删除确认列表,一个做tableview编辑的判断,判断是否可以选择,判断列表是否选中
3.按钮的点击事件(编辑,全选,确认,删除)
4.获取到服务器数据列表list,赋值给待选中的数组
5.进行与服务器的数组比较,列表的id是否是数组中的某个值,返选时需要。
6.功能完善,点击确认删除,成功…

注意:

//此行不能加,否则列表点击不会有选择的效果
- wifiCell.selectionStyle = UITableViewCellSelectionStyleNone;

//判断列表有没有选中,没有选中则为false

  • self.wfView.wfTableView.isEditing == false

三:代码

1.样式h

#import <UIKit/UIKit.h>
#import "BaseView.h"

@protocol WifiViewDelegate <NSObject>

-(void)onTapCilck;//点击全选/返选

-(void)onTapCancel;//确认删除

@end

@interface WifiView : BaseView

@property(weak,nonatomic) id<WifiViewDelegate> delegate;

@property(nonatomic,strong) UIView *bottomView;//下方按钮的view

@property(nonatomic,strong) UITableView *wfTableView;//列表

@property(nonatomic,strong) UILabel * checkAll;//左按钮全选文字

@property(nonatomic, strong) UIButton *checkAll_Btn;//左按钮

@property(nonatomic, strong) UILabel *canCelb;//右按钮删除文字

@property(nonatomic, strong) UIButton *canCel_Btn;//右按钮

@end

2.m逻辑层

ios表格删除 苹果手机如何删除表格_数组_02

//右边的按钮
-(void)customNavigationRightItem{
    _item = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(onSevleClick)];
    _item.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = _item;
}
-(void)onSevleClick{
    
    if(self.wfView.wfTableView.isEditing == false){
            _item.title = @"Cancel";
            [self functionSelect:NO];
            self.wfView.bottomView.hidden = NO;
            self.wfView.checkAll_Btn.hidden = NO;
            self.wfView.canCel_Btn.hidden = NO;
    }else{
            _item.title = @"Edit";
            [self functionSelect:YES];
            self.wfView.bottomView.hidden = YES;
            self.wfView.checkAll_Btn.hidden = YES;
            self.wfView.canCel_Btn.hidden = YES;
    }
}
-(void)functionSelect:(BOOL)tile{
    if(tile){
  
        _isInsertEdit = tile;
        for (int i = 0; i< _houList.count; i++) {
          NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
          [self.wfView.wfTableView deselectRowAtIndexPath:indexPath animated:NO];
         }
        [self.deleteArray removeAllObjects];
        [self.wfView.wfTableView setEditing:NO animated:YES];
    }else{

        self.wfView.checkAll.text = @"Select All";
        _isInsertEdit = tile;
        [self.wfView.wfTableView setEditing:YES animated:YES];
    }
}

点击左边的全选/返选方法

-(void)onTapCilck{

    if(_isInsertEdit) {
        _isInsertEdit = NO;
        self.wfView.checkAll.text = @"Select All";
        [self.deleteArray removeAllObjects];
        
        [self.wfView.wfTableView setEditing:YES animated:YES];
        [self.wfView.wfTableView reloadData];
    }else {
        self.wfView.checkAll.text = @"Reselect";
        _isInsertEdit = YES;
        [self.deleteArray removeAllObjects];
        for (int i = 0; i< self.houList.count; i++) {
            
            Devices *model = [_houList objectAtIndex:i];
            [self.deleteArray addObject:model.deviceId];
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
            [self.wfView.wfTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
        }
        [self.wfView.wfTableView setEditing:YES animated:YES];
    }
}
//删除的按钮事件
-(void)onTapCancel{
    NSLog(@"onTapCancel==onTapCancel======%@",_deleteArray);
    
    //_deleteArray待删除确认的数组要大于0,否则提示请选中。
    
    if(_deleteArray.count > 0){
        NSString *tips = @"Are you sure to continue deleting?";
        NSString *all = [NSString stringWithFormat:@"%@ %lu set of selected equipment list",tips,(unsigned long)_deleteArray.count];
        CustomView *csview = [[CustomView alloc]initBackroundImage:@"beijingkuang_tuan" Title:@"Delete Tips" contentString:all sureButtionTitle:@"Confirm" cancelButtionTitle:@"Cancel"];
        [csview setSureBolck:^(BOOL clickStatu) {
         
            for(int i=0; i<self.deleteArray.count; i++){
                [self.wfModel deleteDevice:self.deleteArray[i]];
            }
            [WHToast showMessage:@"successfully deleted" originY:500 duration:2 finishHandler:^{
            }];
        }];
    }else{
        [WHToast showMessage:@"Please select the device list to delete" originY:500 duration:2 finishHandler:^{
        }];
    }
}
/*
    代理
 */
 //这里就是接收服务器的数据,赋值给houList数据列表
-(void)getDataSuccess:(NSArray*_Nullable) resultes{
    [self hideHud];
    [self.refreshControl endRefreshing];
    self.wfView.netView.hidden = YES;
    [self.wfView.wfTableView.mj_header endRefreshing];
   if(resultes.count>0){
        self.houList = [NSMutableArray arrayWithArray:resultes];
        self.dataList = [NSMutableArray arrayWithArray:resultes];
        [self.wfView.wfTableView reloadData];
   }else{
       [WHToast showMessage:@"The data is empty." originY:500 duration:2 finishHandler:^{
        }];
   }
}

//关于Tableview的一些方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WifiCell *wfCell = [tableView dequeueReusableCellWithIdentifier:@CELLIDENTIFITER];
    
    if (wfCell == nil) {
        wfCell = [[WifiCell alloc] initWithStyle:UITableViewCellStyleDefault
            reuseIdentifier:@CELLIDENTIFITER];
    }
    

    if([_houList count]>0){
        Devices *model = [_houList objectAtIndex:indexPath.row];
        wfCell.deviceAddre.text = model.numId;
        wfCell.deviceName.text = model.deviceId;
    }
    return wfCell;
}
#pragma tableView的点击事件 进行到下一个界面的时候
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //取消按下去后会有一个颜色
    //[tableView deselectRowAtIndexPath:indexPath animated:NO];
    static NSString *cellIdentifiter = @"WifiCellIdentifiter";
    WifiCell *wifiCell= [tableView dequeueReusableCellWithIdentifier:cellIdentifiter];

    if (wifiCell == nil) {
        wifiCell = [[WifiCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifiter];
        //wifiCell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    if(_houList.count > 0){
        Devices *model = [_houList objectAtIndex:indexPath.row];
        NSLog(@"model.deviceId======%@",model.deviceId);
        //判断列表如果没有进入编辑状态,点列表就进入下一个页面。否则选中当前某个指定列表
        if(self.wfView.wfTableView.isEditing == false) {
              NSUInteger row = [indexPath row];
              if(row<_houList.count){
                 // Devices *model = [_houList objectAtIndex:indexPath.row];
                  HandlerViewController *blue = [[HandlerViewController alloc] init];
                  blue.deviceId=model.deviceId;
                  blue.title = @"Date";
                  [self.navigationController pushViewController:blue animated:YES];
              }
        }else{
            NSLog(@"选择当前的列表======");
            //print("")
            [self.deleteArray addObject:model.deviceId];
             //_deleteArray.append(!)
        }
        
    }
}
//此方法,是当选中某个指定列表后,返选取消选中的时候。就进入此方法。
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    //取消按下去后会有一个颜色
    //
    static NSString *cellIdentifiter = @"WifiCellIdentifiter";
    WifiCell *wifiCell= [tableView dequeueReusableCellWithIdentifier:cellIdentifiter];

    if (wifiCell == nil) {
        wifiCell = [[WifiCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifiter];
        //无色
        //wifiCell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    if(_deleteArray.count > 0){
        
        Devices *model = [_houList objectAtIndex:indexPath.row];
        NSLog(@"model.deviceId======%@",model.deviceId);
        //循环进行比较,服务器的列表数据和当前需要返选移除的列表id
        for(int i=0; i<_deleteArray.count; i++){
            if([model.deviceId containsString:_deleteArray[i]]){
                //[self.houList addObject:model];
                [self.deleteArray removeObject:[_deleteArray objectAtIndex:i]];
            }
        }
    }
    
    //[self.wfView.wfTableView deselectRowAtIndexPath:indexPath animated:NO];
    //self.wfView.wfTableView.deselectRow(at: indexPath,animated:true);
}

到此为止讲述完毕!