ios – 如何使用列标题和行标题创建UICollectionView?

ios – 如何使用列标题和行标题创建UICollectionView?,第1张

概述我想创建一个如下所示的UICollectionView: 它不可滚动或可编辑.我目前想知道如何为此编写布局.我猜它不会是UICollectionViewFlowLayout的子类.我可以想到很多方法,但如果有任何“正确”的方式,我很好奇.细胞将具有动画效果. 每行或每列应该是它自己的部分吗? 我已经用UICollectionViewFlowLayout的子类完成了你想要的东西.看起来像这样, @i 我想创建一个如下所示的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?所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1057446.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存