您的位置:首页 > 移动开发 > IOS开发

iOS 之 列表联动

2017-02-10 09:25 393 查看

左侧是tableview,右侧是collectionView

其实原理很简单:就是左侧放一个tableview,右侧放一个collectionView(也可以是tableview),只要搞清楚点击表,或者滑动collection的时候,另一个做出相应的效果来就好了

先放上效果图:



主要逻辑代码:

1.首先是点击tableview的时候,要计算出collectionView要滚动到的位置:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

// 计算出 右侧 collectionView 将要 滚动的 位置
NSIndexPath *moveToIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.row];
[self.collectionView selectItemAtIndexPath:moveToIndexPath animated:YES scrollPosition:UICollectionViewScrollPositionTop];
}


2.然后是滑动collectionView的时候,计算tableview要响应的位置:

- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath {

if ([elementKind isEqualToString:UICollectionElementKindSectionHeader]) {

if (indexPath.section!=0) {
NSIndexPath *tabIndexPath = [NSIndexPath indexPathForRow:indexPath.section - 1 inSection:0];
[self.tableView selectRowAtIndexPath:tabIndexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
}
}


Demo地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS列表联动