外卖类双TableView联动

实现入饿了么外卖的双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];
}
}

其实还是比较简单的,理清一下思路就可以了

下面贴上demo地址
https://github.com/WeifengShi1/SWFTwoTableView

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容