HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 如何使用列标题和行标题创建UICollectionView?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想创建一个如下所示的UICollectionView:

它不可滚动或可编辑.我目前想知道如何为此编写布局.我猜它不会是UICollectionViewFlowLayout的子类.我可以想到很多方法,但如果有任何“正确”的方式,我很好奇.细胞将具有动画效果.

每行或每列应该是它自己的部分吗?

解决方法

我已经用UICollectionViewFlowLayout的子类完成了你想要的东西.看起来像这样,

@implementation MultpleLineLayout {
    NSInteger itemWidth;
    NSInteger itemHeight;
}

-(id)init {
    if (self = [super init]) {
        itemWidth = 80;
        itemHeight = 80;
    }
    return self;
}

-(CGSizE)collectionViewContentSize {
    NSInteger xSize = [self.collectionView numberOfItemsInSection:0] * (itemWidth + 2); // the 2 is for spacing between cells.
    NSInteger ySize = [self.collectionView numberOfSections] * (itemHeight + 2);
    return CGSizeMake(xSize,ySizE);
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemATindexPath:(NSIndexPath *)path {
    UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:path];
    NSInteger xValue;
    attributes.size = CGSizeMake(itemWidth,itemHeight);
    xValue = itemWidth/2 + path.row * (itemWidth +2);
    NSInteger yValue = itemHeight + path.section * (itemHeight +2);
    attributes.center = CGPointMake(xValue,yvalue);
    return attributes;
}


-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {
    NSInteger minRow =  (rect.origin.x > 0)?  rect.origin.x/(itemWidth +2) : 0; // need to check because bounce gives negative values  for x.
    NSInteger maxRow = rect.size.width/(itemWidth +2) + minRow;
    NSMutableArray* attributes = [NSMutableArray array];
    for(NSInteger i=0 ; i < self.collectionView.numberOfSections; i++) {
        for (NSInteger j=minRow ; j < maxRow; j++) {
            NSIndexPath* indexPath = [NSIndexPath indexPathForItem:j inSection:i];
            [attributes addObject:[self layoutAttributesForItemATindexPath:indexPath]];
        }
    }
    return attributes;
}

数据被排列为阵列阵列,其中每个内部阵列提供一个水平行的数据.使用我现在的值,并以您的数据为例,视图看起来像这样,

这是我没有视图控制器的代码,

@interface ViewController ()
@property (strong,nonatomiC) UICollectionView *collectionView;
@property (strong,nonatomiC) NSArray *theData;
@end

@implementation ViewController

- (void)viewDidLoad {
    self.theData = @[@[@"",@"A",@"B",@"C"],@[@"1",@"115",@"127",@"132"],@[@"2",@"",@"153"],@[@"3",@"199",@""]];

    MultpleLineLayout *layout = [[MultpleLineLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.collectionView.datasource = self;
    self.collectionView.delegate = self;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.view.BACkgroundColor = [UIColor blackColor];
    [self.view addSubview:self.collectionView];
    [self.collectionView registerNib:[UINib nibWithNibName:@"CustomDataCell" bundle:nil] forCellWithReusEIDentifier:@"DataCell"];
}


- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
    return [self.theData[section] count];
}

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
    return [self.theData count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView  cellForItemATindexPath:(NSIndexPath *)indexPath {
    DataCell *cell = [collectionView  dequeueReusableCellWithReusEIDentifier:@"DataCell" forIndexPath:indexPath];
    cell.label.text = self.theData[indexPath.section][indexPath.row];
    return cell;
}

大佬总结

以上是大佬教程为你收集整理的ios – 如何使用列标题和行标题创建UICollectionView?全部内容,希望文章能够帮你解决ios – 如何使用列标题和行标题创建UICollectionView?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。