
方一:利用collectionView和tableView实现联动.(以后更新)
方法二:利用两个tablview实现联动。
我的想法可能有点奇特啊,哈哈,上面是个小tableview,将tableView和tableViewCell进行了了角度旋转,
废话不多说上代码。
创建两个tableView
self.topTableView = [[BaseTableView alloc]initWithFrame:CGRectMake(0, 0, 40, SCREEN_WIDTH) style:UITableViewStylePlain];
[self.view addSubview:_topTableView];
self.topTableView.delegate = self;
self.topTableView.dataSource = self;
self.topTableView.showsVerticalScrollIndicator = NO;
[self.topTableView registerClass:[BBAllZhuanChangTableViewCell class] forCellReuseIdentifier:@"top"];
self.topTableView.transform = CGAffineTransformMakeRotation((-M_PI_2));//将tableView进行了90旋转
self.topTableView.x = 0;
self.topTableView.y = 0;
self.topTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// self.topTableView.frame = CGRectMake(0,100,SCREEN_WIDTH, 40);
//默认选中第0个cell
[_topTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
self.tableView = [[BaseTableView alloc]initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_HEIGHT - 40) style:UITableViewStylePlain];
[self.view addSubview:_tableView];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
我们还需要将上部分cell进行角度旋转 ,tableView代理方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == _topTableView) {
BBAllZhuanChangTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"top" forIndexPath:indexPath];
cell.nameLabel.text = self.types[indexPath.row];
cell.transform = CGAffineTransformMakeRotation(M_PI_2);
return cell;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
return cell;
}
先实现滑动下面大tableView联动
// 当前的 tableView 是 tableView,tableView 滚动的方向向上,tableView 是用户拖拽而产生滚动的((_isScrollDown主要判断 tableView 用户拖拽而滚动的,还是点击 topTableView 而滚动的)
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
if ( (tableView == self.tableView) && !_isScrollDown && (self.tableView.dragging || self.tableView.isDragging)) {
NSLog(@"向上-----%ld",section);
[self selectRow:section];
}
}
// 当前的 tableView 是 tableView,tableView 滚动的方向向上,tableView 是用户拖拽而产生滚动的((_isScrollDown主要判断 tableView 用户拖拽而滚动的,还是点击 topTableView 而滚动的)
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(nonnull UIView *)view forSection:(NSInteger)section {
if ((tableView == self.tableView) && _isScrollDown && (self.tableView.dragging || self.tableView.isDragging)) {
NSLog(@"向下=====%ld",section + 1);
[self selectRow:section];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 标记一下 self.tableView 的滚动方向,是向上还是向下
if (self.tableView == scrollView) {
static CGFloat lastOffsetY = 0;
if (self.tableView == scrollView) {
_isScrollDown = lastOffsetY < scrollView.contentOffset.y;
lastOffsetY = scrollView.contentOffset.y;
}
}
//拖动下边tableView处理
- (void)selectRow:(NSInteger)index {
if (index <= self.types.count - 1) {
[self.topTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
}
点击上面tableView联动
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == _topTableView) {
self.selectedIndex = indexPath.row;
[self scrollToTop:_selectedIndex animate:YES];
[_topTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_selectedIndex inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
//[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:_selectedIndex] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
}
//点击上面tableview处理
- (void)scrollToTop:(NSInteger)section animate:(BOOL)isanimated {
CGRect headerRect = [self.tableView rectForSection:section];
//NSLog(@"%f--%f",headerRect.origin.y,self.tableView.contentInset.top);
CGPoint topOfHeadr = CGPointMake(0, headerRect.origin.y);
[self.tableView setContentOffset:topOfHeadr animated:YES];
}
大概和核心代码。好的还有自定cell的处理,一起都粘贴出来吧
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
self.highlighted = selected;
self.nameLabel.highlighted = selected;
self.redView.hidden = !selected;
// Configure the view for the selected state
}
ok了,代码没有整理,写在项目了,有需要的我可以给你个demo。好久没写文章。随便写写吧。希望对大家能有帮助。
菜鸡一个,努力学习!
网友评论