左侧是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];
}
}
}