iOS 之 列表联动

左侧是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地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值