iOS双TableView 联动

1. 外层UITableView 滚动关键代码
- (void)viewDidLoad {
    [super viewDidLoad]; 
    self.canScroll = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeScrollStatus) name:@"BottomTableLeaveTopNotification" object:nil];
}
#pragma mark - Notification
- (void)changeScrollStatus//改变主视图的状态
{
    self.canScroll = YES;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat bottom = [self.tvContent rectForSection:1].origin.y - (IS_IPHONE_X?24:0); 
    if (scrollView.contentOffset.y>=bottom) {//当subcell还没有滚动到
        scrollView.contentOffset = CGPointMake(0, bottom);
        if (self.canScroll) {
            self.canScroll = NO;
            //通知子table滚动
            [[NSNotificationCenter defaultCenter] postNotificationName:@"BottomTableScrollChangedNotification" object:nil userInfo:nil];
        }
    }else{
        if (!self.canScroll) {//子cell没到顶
            scrollView.contentOffset = CGPointMake(0, bottom);
        }
    }
}
2. 内层UITableView/UICollectionView 滚动关键代码
- (void)viewDidLoad {
    [super viewDidLoad];  
    //滚动处理
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeScrollStatus:) name:BottomTableScrollChangedNotification object:nil];
}

#pragma mark - Notification
-(void)changeScrollStatus:(NSNotification *)objc
{
    _canScroll = YES;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.canScroll == NO)
    {
        scrollView.contentOffset = CGPointZero;
    }
    if (scrollView.contentOffset.y <= 0)
    {
        self.canScroll = NO;
        scrollView.contentOffset = CGPointZero;
        //通知外层table滚动
        [[NSNotificationCenter defaultCenter] postNotificationName:BottomTableLeaveTopNotification object:nil];
    }
}

源码在此* demo

你可能感兴趣的:(iOS双TableView 联动)