使用 UICollectionView 的类似电子表格的布局

aza*_*arp 5 ios

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

例子:

Customer Id   First Name   Last Name   Social  Address  Phone 
Run Code Online (Sandbox Code Playgroud)

这是我想出的使用 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;
}
Run Code Online (Sandbox Code Playgroud)

这是输出:

在此处输入图片说明

lxt*_*lxt 2

我不确定您是否能够适应标准流程布局 - 您可能需要创建自己的UICollectionViewLayout类 - 但这应该相当简单。

与表格视图类似,集合视图可以包含部分和项目:电子表格的每一行都可以是一个部分,然后各个元素(姓氏、社交地址、地址等)可以是项目。

如果您想要真正的电子表格之类的功能(即,您可以水平垂直滚动),那么您将必须构建自己的布局类。否则,您可能能够适应现有的流程布局,尽管可以说,如果您不需要滚动,您基本上可以使用表视图来实现您所追求的目标。