iOS tableView与collectionView的样式切换以及间距问题处理

#define ScreenWidth ([UIScreen mainScreen].bounds.size.width)

#define Screenheight ([UIScreen mainScreen].bounds.size.height)

@property (weak,nonatomic) IBOutletUICollectionView *collectionView;

@property (nonatomic,assignBOOL isGrid;

staticNSString *scell = @"celll";

staticNSString *fcell =@"cell";


- (void)setNavtion {

    //    self.automaticallyAdjustsScrollViewInsets=NO;//去掉顶部留白

    self.navigationItem.rightBarButtonItem=[[UIBarButtonItemalloc]initWithTitle:@"网格"style:(UIBarButtonItemStyleDone)target:selfaction:@selector(onBtnClick:)];

}


- (void)setCollection {

    _collectionView.delegate=self;

    _collectionView.dataSource =self;

    // 默认列表视图

    _isGrid =NO;

    // 注册cell

    [_collectionViewregisterClass:[UICollectionViewCellclass] forCellWithReuseIdentifier:scell];

    [_collectionViewregisterClass:[UICollectionViewCellclass] forCellWithReuseIdentifier:fcell];

}


- (void)onBtnClick:(UIBarButtonItem *)sender {

    _isGrid = !_isGrid;

    [self.collectionViewreloadData];

    

    if (_isGrid) {

        sender.title =@"线格";

    } else {

        sender.title =@"网格";

    }

}


#pragma mark UICollectionViewDataSource

// cell个数

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return30;

}


// cell样式

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    if (_isGrid) {

       //在这里注册自定义的XIBcell否则会提示找不到标示符指定的cell

        UINib *nib = [UINibnibWithNibName:@"SCollectionViewCell"bundle: [NSBundlemainBundle]];

        [collectionViewregisterNib:nib forCellWithReuseIdentifier:scell];

        SCollectionViewCell *cell =[collectionViewdequeueReusableCellWithReuseIdentifier:scellforIndexPath:indexPath];

        cell.label.text =@"姓名:90909090";

        cell.labelsecond.text =@"电话:456a4dasd";

        cell.iamgeView.image=[UIImageimageNamed:@"003"];

        return cell;

    } else {

       //在这里注册自定义的XIBcell否则会提示找不到标示符指定的cell

        UINib *nib = [UINibnibWithNibName:@"FCollectionViewCell"bundle: [NSBundlemainBundle]];

        [collectionViewregisterNib:nib forCellWithReuseIdentifier:fcell];

        FCollectionViewCell *cell = [collectionViewdequeueReusableCellWithReuseIdentifier:fcellforIndexPath:indexPath];

        cell.label.text =@"姓名:61465456456";

        cell.labelsecond.text =@"电话:86465614";

        cell.iamgeView.image=[UIImageimageNamed:@"003"];

        //        cell.layer.borderColor=CFBridgingRetain(([UIColor darkGrayColor]));

        //        cell.layer.borderWidth=0.3;

        return cell;

    }

}


// cell的大小 CGSizeMake

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"开始啊");

    if (_isGrid) {

        NSLog(@"九宫格");

        returnCGSizeMake((ScreenWidth -30) / 2, (ScreenWidth -6) / 2 +40-40);

    } else {

        NSLog(@"条行格");

        returnCGSizeMake(ScreenWidth -4, (ScreenWidth -6) / 4 +20-20);

    }

}


// 四边间距 UIEdgeInsetsMake

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {

    if (_isGrid) {

        NSLog(@"九宫格");

         returnUIEdgeInsetsMake(10,10, 10,10);//分别为上、左、下、右边距

    } else {

        NSLog(@"条行格");

         returnUIEdgeInsetsMake(2,10, 2,10);//分别为上、左、下、右边距

    }

}


//设置水平间距 (同一行的cell的左右间距)

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {

    if (_isGrid) {

        NSLog(@"九宫格");

        return10;

    } else {

        NSLog(@"条行格");

        return2;

    }

}


//垂直间距 (同一列cell上下间距)

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {

    if (_isGrid) {

        NSLog(@"九宫格");

         return10;

    } else {

        NSLog(@"条行格");

         return2;

    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值