美文网首页
UICollectionView+UITableView滑动范围

UICollectionView+UITableView滑动范围

作者: 守护地中海的花 | 来源:发表于2019-06-15 16:54 被阅读0次

1.控制器中占位图在UICollectionView和UITableView没有数据时展示 滑动范围受限
2.特殊要求如下图已经到底了需要脚步或者说可以直接扩大范围


image.png

提前总结:UICollectionView扩大滑动范围靠sectionInset的top、bottom如果横向则靠left、bottom。UITableView直接改变contentSize即可,或者加入footer

UICollectionView

1.影响滑动范围有Cell数量、minimumLineSpacing 、sectionInset的2个属性top,bottom、头部(垂直滑动 水平滑动sectionInset的2个属性left,right)
2.特别注意一点就是 如果cell数量不足 以及sectionInset属性不够大屏幕就显示完全 mainView是不可以滑动的 但是UITableView下mainView可以滑动的

  • Cell数量、minimumLineSpacing

1.设置top、bottom为0、minimumLineSpacing为0、没有头部
2.cell大小设置

//一行2个cell
flowLayout.itemSize = CGSizeMake((WIDTH - 30)*0.5, 200);

3.1个cell 滑动范围就是200、3个cell400
4.如果设置minimumLineSpacing50 一个cell 200 3个cell450---说明cell个数必须有换行的数量 minimumLineSpacing50才可以和cell一起滑动mainView滑动范围

  • sectionInset属性top bottom (适用于展位图改变bottom)

1.设置cell数量2个(默认滑动范围200)、minimumLineSpacing为0、没有头部---top,bottom和cell个数无关的 都会直接影响mainView滑动范围

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%f",self.mainView.contentSize.height);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.flowLayout.sectionInset = UIEdgeInsetsMake(1000, 0, 100, 0);
    //[self.mainView reloadData];
    NSLog(@"%f",self.mainView.contentSize.height);
}

3.如果不点击空白处单独点击cell或者滑动高度200 点击空白处 不需要reloadData 直接就改变了滑动范围1300(一个cell高度+top+bottom)

  • 头部

1.设置cell数量2个(默认滑动范围200)、minimumLineSpacing为0、insetionInsetTop,bottom为 0

[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(WIDTH, 200);
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
        view.backgroundColor = [UIColor orangeColor];
        return view;
    } else {
        return nil;
    }
}

3.点击cell获取滑动范围高度是400

UITableView

1.影响滑动范围有Cell数量、头部
2.特别注意一点就是 如果cell数量不足 mainView是可以滑动的 但是UICollectionView下mainView不可以滑动的

  • Cell数量

1.没有头部
2.cell大小设置

tableView.rowHeight = 200;

3.1个cell 滑动范围就是200、3个cell600

  • 头部

1.设置cell数量2个(默认滑动范围400)

UIView *head = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 100)];
    head.backgroundColor = [UIColor purpleColor];
    self.mainView.tableHeaderView = head;

3.点击cell获取滑动范围高度是500

最核心的就是直接改变ContentSize看看效果

  • UICollectionView
    默认2个cell 高度400
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.mainView.contentSize = CGSizeMake(0, 1000);
    NSLog(@"%f",self.mainView.contentSize.height);
}
总结:瞬间改变高度 1000但是不可以 滑动,一旦滑动点击cell滑动范围回归正常 400 很尴尬不行
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.mainView.contentSize = CGSizeMake(0, 1000);
    [self.mainView reloadData];
    NSLog(@"%f",self.mainView.contentSize.height);
}
总结:reloadData 还是不可以滑动 范围不变任然是400

总结:不管是否reload都不能直接改变滑动范围、 不reload之后滑动cell 即使没有cell滑动mainView虽然没有啥效果但是此时高度恢复默认 、reload之后一直都是默认了、占位使用改变sectionInset的bottom,top

  • UITableView
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%f",self.mainView.contentSize.height);
    self.mainView.contentSize = CGSizeMake(0, 1000);
    NSLog(@"%f",self.mainView.contentSize.height);
}
总结:直接改变UITableView的滑动范围 可取
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%f",self.mainView.contentSize.height);
    self.mainView.contentSize = CGSizeMake(0, 1000);
    [self.mainView reloadData];
    NSLog(@"%f",self.mainView.contentSize.height);
}
总结:如果加reloadData恢复是默认是44的cell数量倍数 比如3个cell 高度300(cell高度100)改变高度之后 reload 变成44*3=132高度 但是点击cell重新获取高度变回300
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.mainView.contentSize = CGSizeMake(0, 1000);
    [self.mainView.mj_header endRefreshing];
    [self.mainView.mj_footer endRefreshing];
    NSLog(@"%f",self.mainView.contentSize.height);
}
总结:停止滑动啥的 不改变滑动范围

总结:不要reload不然回复原样了

添加footer(最简单)

封装一个Mj系列的脚步

  • .h
#import "MJRefreshAutoStateFooter.h"

NS_ASSUME_NONNULL_BEGIN

@interface ShopRushTopSonFooter : MJRefreshAutoStateFooter

@end

NS_ASSUME_NONNULL_END
  • .m
#import "ShopRushTopSonFooter.h"
@interface ShopRushTopSonFooter ()
@property(nonatomic,strong)UIView *leftView;
@property(nonatomic,strong)UILabel *centerLab;
@property(nonatomic,strong)UIView *rightView;
@end

@implementation ShopRushTopSonFooter
- (void)prepare
{
    [super prepare];
    self.height = 200*ADAPTER_WIDTH;
    self.stateLabel.hidden = YES;
    self.centerLab.text = @"已经到底了,没有更多了";
    [self leftView];
    [self rightView];
}
#pragma mark - lazy懒加载
- (UILabel *)centerLab
{
    if (!_centerLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.font = [UIFont systemFontOfSize:12*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = kColor152;
        lab.textAlignment = NSTextAlignmentCenter;
        lab.numberOfLines = 1;
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(10*ADAPTER_WIDTH);
            make.centerX.mas_equalTo(0);
        }];
        _centerLab = lab;
    }
    return _centerLab;
}
- (UIView *)leftView
{
    if (!_leftView) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = kColor152;
        [self addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(21*ADAPTER_WIDTH, 0.5));
            make.right.mas_equalTo(self.centerLab.mas_left).mas_equalTo(-5.5*ADAPTER_WIDTH);
            make.centerY.mas_equalTo(self.centerLab);
        }];
        _leftView = view;
    }
    return _leftView;
}
- (UIView *)rightView
{
    if (!_rightView) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = kColor152;
        [self addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(21*ADAPTER_WIDTH, 0.5));
            make.left.mas_equalTo(self.centerLab.mas_right).mas_equalTo(5.5*ADAPTER_WIDTH);
            make.centerY.mas_equalTo(self.centerLab);
        }];
        _rightView = view;
    }
    return _rightView;
}
@end

  • 使用方法
MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
[footer setTitle:@"正在努力加载" forState:MJRefreshStateIdle];
self.mainView.mj_footer = footer;

相关文章

网友评论

      本文标题:UICollectionView+UITableView滑动范围

      本文链接:https://www.haomeiwen.com/subject/fppbfctx.html