电子表格就像使用UICollectionView的布局

如何使用UICollectionView创build电子表格,如每行显示单个对象属性的布局。

例:

Customer Id First Name Last Name Social Address Phone 

下面是我使用UITableView的想法:

 funds = [[NSMutableArray alloc] init]; columnNames = [NSArray arrayWithObjects:@"fundNumber",@"fundName",@"unitPrice",@"fundBalance",@"percentageOfAccountValue",@"futureContributionAllocation", nil]; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FundCell"]; Fund *fund = [funds objectAtIndex:[indexPath row]]; for(NSString *columnName in columnNames) { UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, 0, 100, 44)]; label.backgroundColor = [UIColor clearColor]; label.text = [fund valueForKey:columnName]; x += label.bounds.size.width; [cell addSubview:label]; } x = 0; return cell; } 

这里是输出:

在这里输入图像说明

我不确定你是否能够适应标准的stream布局 – 你可能需要创build你自己的UICollectionViewLayout类 – 但是这应该是相当简单的。

像表格视图一样,您的collections视图可以包含部分和项目:电子表格的每一行都可以是一个部分,然后各个元素(姓,社交,地址等)可以是项目。

如果你想要真正的电子表格function(例如,你可以水平垂直滚动),那么你将不得不build立自己的布局类。 否则,你可能会适应现有的stream程布局,虽然可以说,如果你不需要滚动你可以基本上实现你的表后,而不是。

您可以使用UICollectionViewFlowLayout自定义UICollectionViewController的布局您想做的背后的想法是这样的:每一列可以是一个部分,每个部分将是每个部分的标题。

例如: – numberOfSectionsInCollectionView: = 6(客户ID,名字,姓氏,社交,地址,电话) – collectionView:numberOfItemsInSection: =行数