效果圖.gif
方一:利用collectionView和tableView實現(xiàn)聯(lián)動.(以后更新)
方法二:利用兩個tablview實現(xiàn)聯(lián)動。
我的想法可能有點奇特啊,哈哈,上面是個小tableview,將tableView和tableViewCell進行了了角度旋轉(zhuǎn),
廢話不多說上代碼。
創(chuàng)建兩個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旋轉(zhuǎn)
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進行角度旋轉(zhuǎn) ,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;
}
先實現(xiàn)滑動下面大tableView聯(lián)動
// 當前的 tableView 是 tableView,tableView 滾動的方向向上,tableView 是用戶拖拽而產(chǎn)生滾動的((_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 是用戶拖拽而產(chǎn)生滾動的((_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聯(lián)動
- (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。好久沒寫文章。隨便寫寫吧。希望對大家能有幫助。
菜雞一個,努力學習!