实现tableView和collectionView的相互关联方法

来源:互联网 发布:阿里云ssh登录 编辑:程序博客网 时间:2024/06/01 18:44

最近比较忙,没事帮别人做个小Demo,其实很简单.不用多少时间,看看核心实现吧.

用到的方法

#pragma mark 返回头视图 / 尾视图 通过kind判断

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath


//这个就不多说了

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    if (scrollView ==self.collectionView) {

        if (isGetWhatWay ==YES) {

            // collectionView在控制器view的中心点转化成collectionView上的坐标

            CGPoint pInView = [selfconvertPoint:self.collectionView.centertoView:self.collectionView];

            // 获取这一点的indexPath

            NSIndexPath *indexPathNow = [self.collectionViewindexPathForItemAtPoint:pInView];

            if (indexPathNow.section ==0) {

                //获取所有collectionView的头视图(也可用item)

                NSArray *array = [self.collectionViewindexPathsForVisibleSupplementaryElementsOfKind:UICollectionElementKindSectionHeader];

                if ([[arrayfirstObject]section] ==0 || [[arrayfirstObject]section] ==1) {

                    NSIndexPath *tabIndexPath = [NSIndexPathindexPathForRow:0inSection:0];

                    [self.tableViewselectRowAtIndexPath:tabIndexPathanimated:NOscrollPosition:UITableViewScrollPositionNone];

                    [self.tableViewscrollToRowAtIndexPath:tabIndexPathatScrollPosition:UITableViewScrollPositionTopanimated:YES];

                }

                return;

            }

            NSIndexPath *tabIndexPath = [NSIndexPathindexPathForRow:indexPathNow.sectioninSection:0];

            [self.tableViewselectRowAtIndexPath:tabIndexPathanimated:NOscrollPosition:UITableViewScrollPositionNone];

            [self.tableViewscrollToRowAtIndexPath:tabIndexPathatScrollPosition:UITableViewScrollPositionTopanimated:YES];

        }

    }

}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

    if (scrollView ==self.collectionView) {

        isGetWhatWay =YES;

    }

}

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

    isGetWhatWay =NO;

    NSIndexPath *index = [NSIndexPathindexPathForRow:0inSection:indexPath.row];

    [self.collectionViewscrollToItemAtIndexPath:indexatScrollPosition:UICollectionViewScrollPositionCenteredVerticallyanimated:YES];

}



#warning 注册头视图

        [_collectionViewregisterClass:[UICollectionReusableViewclass]forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"HeaderCollectionReusableView"];

        [_collectionViewregisterClass:[UICollectionReusableViewclass]forSupplementaryViewOfKind:UICollectionElementKindSectionFooterwithReuseIdentifier:@"FooterCollectionReusableView"];



#pragma mark 返回头视图的高度

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section

#pragma mark 返回尾视图的高度

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section




0 0
原创粉丝点击