iOS 单元格列表&方格的切换

列表与九宫格 随意切换

1,用一个collectionView即可

2,设置BOOL来辨别是列表形式还是九宫格

3,根据BOOL值,用这个方法设置所要显示的每个cell的宽高

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

{

    if (_isGrid)

    {

        return CGSizeMake(([UIScreen mainScreen].bounds.size.width - 10) / 2, ([UIScreen mainScreen].bounds.size.width - 6) / 2 + 40);

    } else

    {

        return CGSizeMake([UIScreen mainScreen].bounds.size.width, ([UIScreen mainScreen].bounds.size.width - 6) / 4 + 20);

    }

}

4,cell里,设置cell根据BOOL值的变化改变布局,然后设置数据

- (void)setIsGrid:(BOOL)isGrid

{

    _isGrid = isGrid;


    if (isGrid)

    {

        NSLog(@"11111111111111111");

        _imageV.frame = CGRectMake(5, 5, self.bounds.size.width - 60, self.bounds.size.width - 60);

        _titleLabel.frame = CGRectMake(5, self.bounds.size.width - 45, ([UIScreen mainScreen].bounds.size.width - 4)/2, 40);

        _priceLabel.frame = CGRectMake(5, self.bounds.size.width + 5,([UIScreen mainScreen].bounds.size.width - 4)/2, 20);

    }else {

        NSLog(@"222222222222222222");

        _imageV.frame = CGRectMake(5, 5, self.bounds.size.height - 10, self.bounds.size.height - 10);

        _titleLabel.frame = CGRectMake(self.bounds.size.height + 10, 0,([UIScreen mainScreen].bounds.size.width - 4)/2, self.bounds.size.height - 20);;

        _priceLabel.frame = CGRectMake(self.bounds.size.height + 10, self.bounds.size.height - 30, ([UIScreen mainScreen].bounds.size.width - 4)/2, 20);;

    }

}

- (void)setModel:(NSDictionary *)model

{

    _model = model;

    _imageV.image = [UIImage imageNamed:@"1.png"];

    _titleLabel.text = @"你的發表卡吉巴基看吧阿里舉辦";

    _priceLabel.text = @"123123";

}

demo:列表与九宫格切换demo

你可能感兴趣的:(iOS 单元格列表&方格的切换)