UItableView搜索和全部删除

关于iOS中的UItableView,代码中:

   //是否显示搜索背景,如果设置成YES,搜索结果无法点击
    _search.dimsBackgroundDuringPresentation = NO;

系统默认是YES,现在改为NO。

**这里写代码片**
//AppDelegate.m文件
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    ViewController *view = [[ViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:view];
    
    self.window.rootViewController = nav;
    return YES;
}



//ViewController.h文件

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UISearchResultsUpdating>


@end




//ViewController.m 文件
#import "ViewController.h"

@interface ViewController ()
{
    UITableView *_tableView;
    NSMutableArray *_dataArray;
    
    //创建全局的搜索控制器
    
    UISearchController *_search;
    
    //创建一个装着搜索结果的可变数组
    NSMutableArray *_searchResultArray;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self creatData];
    [self creatTableView];
 
    [self creatBarButtons];
    [self creatSearch];
}
- (void)creatSearch{
    
    //实例化搜索控制器
    //搜索控制器的搜索结果,会展示在tableview上,所以,在tableview的代理方法里,要区分当前正在活跃的是正常的tableview还是显示搜索结果的tableview
    _search = [[UISearchController alloc] initWithSearchResultsController:nil];
    //设置_search的属性
    //是否显示搜索背景,如果设置成YES,搜索结果无法点击
    _search.dimsBackgroundDuringPresentation = NO;
    
    //如果有导航控制,可以设置下面这个属性,当点击搜索框的时候,是否隐藏导航栏,默认为NO
    _search.hidesNavigationBarDuringPresentation = YES;
    
    
    //搜索框放在该放的位置上,防止意外发生,那么这个方法一定要写在把搜索框加到tableview的方法前面
    [_search.searchBar sizeToFit];
    
    //search的代理方法<会走tableview的代理方法,还有自身的代理方法>
    _search.searchResultsUpdater = self;
    
    //把搜索框加到tablview的头上
    _tableView.tableHeaderView = _search.searchBar;
    
    //单独设置搜索框的宽高,通常是不会单独设置的
//    _search.searchBar.frame
    
}

- (void)creatBarButtons
{
    //在导航栏左右两侧创建2个按钮
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(editTabelView)];
    
    self.navigationItem.leftBarButtonItem = leftItem;
    
    
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc] initWithTitle:@"AllDELE" style:UIBarButtonItemStylePlain target:self action:@selector(DeleteAllChooseData)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}

- (void)editTabelView
{
    //打开或者关闭tableView的编辑状态
    _tableView.editing = !_tableView.editing;
    
}

- (void)DeleteAllChooseData
{
    //把选中的行 全部删除的方法
    
    //tableView在多选删除里,会自动的把选中的cell的indexpath存放进一个可变数组里进行管理
    NSMutableArray *selectedIndexArray = [NSMutableArray arrayWithArray: [_tableView indexPathsForSelectedRows]];
    
    //这个时候,数组里地元素排序是无序的,所以要先排序
    //array 里装着从小到大排好顺序的indexpath
    NSArray *array = [selectedIndexArray sortedArrayUsingSelector:@selector(compare:)];
    
    //因为删除的时候,数组会自动补位,所以要从后往前删除,所以要倒序删除
    for (NSInteger i = array.count-1; i>=0; i--) {
        //每循环一次,都会得到一个indexpath,根据获得的indexpath,删除数据源里对应的数据
        NSIndexPath *indexPath = array[i];
        [_dataArray removeObjectAtIndex:indexPath.row];
    }
    //删除cell  UI效果
    [_tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationAutomatic];
    
    //删除完之后,关闭编辑状态
    _tableView.editing = NO;
}

- (void)creatData
{
    //先初始化数据源
    _dataArray = [[NSMutableArray alloc] init];
    
    for (int i = 0; i<20; i++) {
        NSString *string = [NSString stringWithFormat:@"第%d行",i];
        [_dataArray addObject:string];
    }
    
}

- (void)creatTableView
{
    //初始化tableView
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    //设置行高
    _tableView.rowHeight = 88;//通常用代理,因为每一行的行高有时候是不一样的
    [self.view addSubview:_tableView];
}


//两个必须实现的代理方法
//返回每组的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_search.active) {
        return _searchResultArray.count;
    }else{
        return _dataArray.count;
    }
    
}

//返回单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"];
    }
    
    //给cell加一个向右指示的箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    //给cell的控件属性赋值
    if (_search.active) {
        cell.textLabel.text = _searchResultArray[indexPath.row];
    }else{
         cell.textLabel.text = _dataArray[indexPath.row];
    }
    
    return cell;
}

//先允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}

//返回tableviewcell 的编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //想要多选删除,那么返回的编辑样式是:
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
    
    
}

//选中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //如果是多选删除的时候,而且还有行点击事件,这个时候,多选删除时优先的。要先把行点击事件关掉
    if (tableView.editing) {
        
        
    }else{
        if (_search.active) {
            NSLog(@"%@",_searchResultArray[indexPath.row]);
        }else
        {
            NSLog(@"%@",_dataArray[indexPath.row]);
        }
        
    }
    
}

//search的搜索工作,是在自身的代理方法里实现的
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    
    //先把搜索框的文本获取到
    NSString *text = searchController.searchBar.text;
    
    //正则表达式之谓词搜索
    //搜索条件
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS [c] %@",text];
    
    if (_searchResultArray != nil) {
        //先把上次的搜索结果清空
        [_searchResultArray removeAllObjects];
    }
    
    _searchResultArray = [NSMutableArray arrayWithArray:[_dataArray filteredArrayUsingPredicate:predicate]];
    
    //让tablevie重新刷新数据,那么所有的data
    [_tableView reloadData];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


运行结果:
这里写图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值