表格横向布局及分页实现原理

第一种方法:采用ZJScrollPageView组件。
1.采用ZJScrollPageView组件;
2.对数据进行横向分页;
3.建立支持ZJScrollPageViewChildVcDelegate协议的页面;
4.在父页面实现childViewController,子UIViewController为第三步建立多个页面。
5.添加底部滑动条。
演示视频下载地址
第二种方法:采用UICollectionView。我做了不分页的情况。据说通过scrollViewDidScroll可以控制一次滑一屏幕,我没有试过。若不分页的情况,这种方案最简单。不过滑动条是系统自带的,滑动结束滑动条消失,若想实现滑动进度条需要自己实现,通过scrollViewDidScroll计算滑动距离显示滑动进度条,实现滑动进度条比较麻烦。具体用那种方法根据具体场景计算。
想实现横向滑动布局最关键的设置代码是:

flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

其次:表格要设计为多section,每个section的个数为1;

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 1;//isCommonUnitEmptyArray(self.model.photosArr) ? 1 : self.model.photosArr.count+1;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return isCommonUnitEmptyArray(self.model.photosArr) ? 1 : self.model.photosArr.count+1;
}

注意:不能像纵向布局UICollectionView那样设置referenceSizeForHeaderInSection和referenceSizeForFooterInSection。一般不设置就可以若想设置就设置高度为1.0f / [UIScreen mainScreen].scale
具体代码:

- (UICollectionView *)collectionView {
    if(!_collectionView)
    {
        //创建布局
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];

        //创建CollectionView
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(45, (FULL_HEIGHT - kNavBarAndStatusBarHeight-30-30-36-15-18-15-18-(40 - itemDetailViewInterval/2)-24 -25-60 -15-18-(45 - itemDetailViewInterval/2)-24-15-18-(55+24+15+18-itemDetailViewInterval)*2)/2 +30+30+36+15+18+15+18+(40 - itemDetailViewInterval/2)+24 +25-10, (kUIScreenWidth-475-45*2), 60+15+18+20) collectionViewLayout:flowLayout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.showsHorizontalScrollIndicator = YES;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
//        _collectionView.alwaysBounceVertical = YES;
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _collectionView.backgroundColor = [UIColor clearColor];//BGColorHex(F9F9F9);
        [_collectionView registerClass:[JCTItemDetailListCell class] forCellWithReuseIdentifier:NSStringFromClass([JCTItemDetailListCell class])];
//        [_collectionView registerClass:[PPFeaturedItemtListCell class] forCellWithReuseIdentifier:NSStringFromClass([PPFeaturedItemtListCell class])];
//        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([PPChooseSongHeadView class])];
//        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([PPFeaturedItemsHeadView class])];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView"];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//        _collectionView.alwaysBounceHorizontal = YES;
//        _collectionView.alwaysBounceVertical = NO;

        //定义每个UICollectionView 的大小
        flowLayout.itemSize = CGSizeMake(111, 60);
        //定义每个UICollectionView 横向的间距
        flowLayout.minimumLineSpacing = BG_1PX;
        //定义每个UICollectionView 纵向的间距
        flowLayout.minimumInteritemSpacing = BG_1PX;
        //定义每个UICollectionView 的边距距
        //    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);//上左下
    }
    return _collectionView;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值