Table table as layout using UICollectionView

How can I use UICollectionView to create a table such as a layout where each row displays the properties of one object.

Example:

Customer Id   First Name   Last Name   Social  Address  Phone 

      

This is what I came up with using a 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;
}

      

And here's the output:

enter image description here

+3


source to share


2 answers


I'm not sure if you can adapt the standard thread layout - you may need to create your own class UICollectionViewLayout

, but it should be pretty simple.

Like a table view, your collection view can have sections and elements: each row of your table can be a section, and individual elements (Last Name, Social, Address, etc.) can be elements.



If you want a true spreadsheet (for example, you can scroll horizontally and vertically), you will need to create your own layout class. Otherwise, you should be able to adapt the existing flow layout, although perhaps if you don't need to scroll, you can basically achieve what you need with a table view.

+2


source


You can customize the layout UICollectionViewController

with UICollectionViewFlowLayout

. The idea of ​​what you want to do is this: every column can be a section every heading will be a heading for every section.



In your example: – numberOfSectionsInCollectionView:

= 6 (Customer ID, First Name, Last Name, Social, Address, Phone) – collectionView:numberOfItemsInSection:

= number of lines

+2


source







All Articles