很多app中都需要城市选择,最近自己写了一个城市筛选、搜索功能,以及tableView双表联动,上拉自动至下一个分类,tableView的折叠布局。
一、双表联动
1、上拉至下一个分类
- 给rightTableView添加footRefresh 在刷新回调的时候进行操作,给leftTableView选中至当前的下一个indexPathRow 并同时将下一个rightTableView分类的dataSource传送过去,并刷新leftTableView、rightTableView两个表。将leftTableView当前选中的indexPath滚动到顶部。
#pragma mark - rightTableView刷新回调
- (void)childTableCallback {
LBWeakSelf(self);
self.childTableView.freshFinishCallback = ^ {
LBStrongSelf(self);
[self setCurrentIndexWithRow:self.currentIndex.row+1];
};
}
- (void)setCurrentIndexWithRow:(NSInteger)row {
NSIndexPath *currentIndex = [NSIndexPath indexPathForRow:row inSection:0];
// 调用leftTableView的didSelectRowAtIndexPath方法实现选中下一个indexPathRow
[self.baseTableView tableView:self.baseTableView.baseTableView didSelectRowAtIndexPath:currentIndex];
// 记录当前选中的indexPath
self.currentIndex = currentIndex;
[self.baseTableView setValue:self.currentIndex forKey:@"selectedIndex"];
[self.baseTableView.baseTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
- 点击leftTableView 将当前的分类数据传送给rightTableView并刷新rightTableView。
#pragma mark - leftTableView点击回调
- (void)baseTableViewCallback {
LBWeakSelf(self);
self.baseTableView.didSelectCellCallback = ^(NSIndexPath *indexPath, UITableViewCell *currentBaseCell) {
LBStrongSelf(self);
LBModel* model = self.dataSource[indexPath.row];
// 数据源传给rightTableView
[self.childTableView setValue:model.cityList forKey:@"childData"];
// 刷新rightTableView
[self.childTableView reload];
self.currentIndex = indexPath;
};
}
2、所有区联动
- 监听rightTableView滚动,并获取当前屏幕第一个section回调给leftTableView进行刷表同时将当前的section滚动到顶部
- (void)scrollwithIndex:(NSIndexPath *)index {
self.selectedIndex = index;
//tableview滚动到指定的行:
[self.baseTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index.row inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
[self.baseTableView reloadData];
}
二、tableView折叠
- 定义BOOL值isFlod 记录是否可以展开,定义currentIndex记录当前展开的index
// 是否可以展开
@property(nonatomic, assign)BOOL isFlod;
// 当前展开的index
@property(nonatomic, assign)NSInteger currentIndex;
```
- 给tableViewSection添加UIButton并设置tag为当前section
```
sectionBtn.tag = section;
sectionBtn.selected = self.currentIndex==section&&self.isFlod==YES?YES:NO;
- Button点击方法设置isFlod值和currentIndex值并刷新tableView
- (void)sectionSelect:(UIButton*)sender {
self.currentIndex = sender.tag;
sender.selected = !sender.selected;
self.isFlod = sender.selected;
[self