美文网首页
tableView的折叠-无聊的可看

tableView的折叠-无聊的可看

作者: 隔壁班小明 | 来源:发表于2016-05-18 10:29 被阅读526次

主要感觉最近没什么好写的~就简单整理一下自己的思路吧,折叠是一个很简单的效果我写写,大家可以看看,,

折叠的实现有几个方法效果比较好的就是把折叠之前的看到的做成section的头视图,把折叠开的部分做成section中的row,加上数据的配合就能实现折叠效果了。

按上面说的那数据的效果应该是数据数组中存放的是字典(字典对应每个section其中必有的两个字段是isOpen:是否下拉被打开,childs:下面rows的数据数组),要是数据对象也是同理可证了。

下面是我做的折叠UITableViewDataSource代理

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{//我使用的是Model

if (self.dataArray[section].isOpen) {//判断是否打开状态,打开状态返回childs数组的个数

return [self.dataArray[section].childs count];

}

return 0;//没打开返回0}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return self.dataArray.count;//返回section的个数

}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

。。。。。。//返回头视图的样式自定义的但是要确保可以点击,,有点击事件,我的头视图上面有个按键,其点击事件为imageButtonClick:

return headView;

}

-(void)imageButtonClick:(UIButton *)button{

if (self.dataArray[button.tag - buttonTagBaseNumber].isOpen) {//buttonTagBaseNumber是一个tag值的基数确保tag不从0开始,将开关变成NO,刷新单个section将列表打开(ps:个人感觉UITableViewRowAnimationNone的刷新效果最好)

self.dataArray[button.tag - buttonTagBaseNumber].isOpen = NO;

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:(button.tag - buttonTagBaseNumber)] withRowAnimation:UITableViewRowAnimationNone];

}else{

//如果是正在打开状态判断当前section下有没有数据如果有数据,将开关变成YES,刷新单个section将列表打开

if ([self.dataArray[(button.tag - buttonTagBaseNumber)].childs count] != 0) {

self.dataArray[button.tag - buttonTagBaseNumber].isOpen = YES;

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:(button.tag - buttonTagBaseNumber)] withRowAnimation:UITableViewRowAnimationNone];

//将打开的section滚动到屏幕中间

NSIndexPath * index = [NSIndexPath indexPathForRow:0 inSection:(button.tag - buttonTagBaseNumber)];

[self.tableView scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionMiddle animated:YES];

}else{//如果section下没有数据,直接给出提示,不做任何操作

[self showMessage:@"亲这个暂无分类~"];

}

}

}

//选中其中一条的时候将section关闭

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPat{

self.dataArray[indexPat.section].isOpen = NO;

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:(indexPat.section)] withRowAnimation:UITableViewRowAnimationNone];

}

相关文章

网友评论

      本文标题:tableView的折叠-无聊的可看

      本文链接:https://www.haomeiwen.com/subject/mrqwrttx.html