TableView的多选删除,搜索

*****多选删除*****

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

{

    //2个一起返回,就是多选删除模式

    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;

}


思路

【一】,设一个可变数组arr,当处于编辑状态时每选中一个cell就添加一个数据到arr中,反选一个就删除一个


【二】,在编辑方法中清除arr中的内容


//self.editButtonItem系统自带的编辑按钮

//使用系统自带的编辑按钮时必须让父类重载编辑方法

-(void)setEditing:(BOOL)editing animated:(BOOL)animated

{

    [super setEditing:editing animated:animated];

    [_myTableView setEditing:editing animated:animated];


}


【三】,设一个button或其它有点击事件的view,在点击事件中将arr里的对象从数据源中删除,然后重载tableView




*****搜索*****


【一,索引】

//tableView设置索引

//NSArray (存放索引标题的数组)

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

    //UITableViewIndexSearch 系统内置的字符串,会显示成一个放在镜

    NSMutableArray *arr = [[NSMutableArray arrayWithObject:UITableViewIndexSearch];

    //do something

    return arr;

}


- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index

{//因为索引的前面加了个搜索小图标,所以需要重写这个方法点击的时候要返回的数要-1

    return index-1;

}



【二,搜索】


//搜索条被激活时会自动出现在导航条上

一,创建UISearchBarUISearchDisplayController



_sb = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];

    _tv.tableHeaderView = _sb;

    

    _sdc = [[UISearchDisplayController alloc]initWithSearchBar:_sb contentsController:self];

    _sdc.delegate = self;

    _sdc.searchResultsDelegate = self;

    _sdc.searchResultsDataSource = self;




二,【UISearchDisplayDelegate


- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString

{//在搜索框里输入的时候触发

    [_resultArr removeAllObjects];

    

    for (NSMutableArray *marr in _dataArr) {

        for (NSString *str in marr) {

            NSRange ran = [str rangeOfString:searchString];

            if (ran.length) {

                [_resultArr addObject:str];

            }

        }

    }

    

    return YES;

}


三,搜索结果展示会自动创建一个新的tableView,用的也是tableView的代理,所以需要在所有的代理中判断现在是搜索结果,还是原来的tableview





折叠,展开


思路:

1,设置一个可变数组记录每一分区的展开状态,点击每一行的headView的时候就改变这一分区的展开状态,然后重载这一段


[_tv reloadSections:[NSIndexSet indexSetWithIndex:sender.tag-1000] withRowAnimation:UITableViewRowAnimationAutomatic];


2,在numberOfRowsInSection里判断展开状态,如果是展开状态就正常显示,如果是折叠状态就返回0





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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值