实现入饿了么外卖的双tableVIew联动
如图
QQ20180903-152751-HD (2).gif
就是
点击左边tableVIew的cell,右边的tableView滑动至指定位置。
滑动右边tableView的cell,左边的tableView滑动至指定位置。
其实重点就是在拖动右侧tableView 左侧目录跟着滑动
主要代码如下
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// 如果是 左侧的 tableView 直接return
if (scrollView == self.leftTableView){
return;
}
/**如果不是主动滑动的直接return*/
BOOL isUserTouch = scrollView.dragging || scrollView.tracking || scrollView.decelerating;
if (!isUserTouch) {
return;
}
// 取出显示在 视图 且最靠上 的 cell 的 indexPath
NSIndexPath *topCellIndexpath = [[self.centerTableView indexPathsForVisibleRows] firstObject];
// 左侧 talbelView 移动到的位置 indexPath
NSIndexPath *moveToIndexpath = [NSIndexPath indexPathForRow:topCellIndexpath.section inSection:0];
// 移动 左侧 tableView 到 指定 indexPath 居中显示
[self.leftTableView selectRowAtIndexPath:moveToIndexpath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
另外还有在点击左侧tableView时,右侧tableView滑动到指定位置
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView == self.leftTableView) {
NSIndexPath *moveToIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.row];
[self.centerTableView selectRowAtIndexPath:moveToIndexPath animated:NO scrollPosition:UITableViewScrollPositionTop];
}
}
其实还是比较简单的,理清一下思路就可以了