我想创建一个如下所示的UICollectionView:
它不可滚动或可编辑。我目前想知道如何为此编写布局。我猜它不会是UICollectionViewFlowLayout
的子类。我可以想到很多方法,但如果有任何“正确”的方式,我很好奇。细胞将具有动画效果。
每行或每列应该是自己的部分吗?
答案 0 :(得分:7)
我已经用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;
}
答案 1 :(得分:0)
你可以用UICollectionViewFlowLayout
来做,但如果你这样做,你需要仔细计算并获得填充(即左上角和其他“空”单元格)。如果你错误计算它是显而易见的,因为流程会迅速破坏所有内容,你会发现你的标题单元格位于列的中间位置。 (我做了类似的事情)
我只是这样做是因为害怕自定义布局 - 但事实上它就像UITableView
一样简单。如果您的布局根本不滚动,那么您的布局将非常简单,因为您唯一的实际工作是计算单元格的帧值,并在layoutAttributesForItemAtIndexPath
中返回。由于您的整个视图都适合可见区域,layoutAttributesForElementsInRect
意味着您只需遍历视图中的所有单元格。 collectionViewContentSize
将是您视图的大小。
查看示例图片,您可能会发现将数据组织为数组字典很方便,每列一个。您可以按名称(“A”,“B”等)获取列数组,并且数组中的位置对应于最左侧列中的值,您可以将其命名为“index”。
您可以使用更多方法,但这些是基础知识,并且可以使您的基本显示正常运行。