10

I am working on a iOS 8 app with Xcode. I need to display a table with many columns and rows of data in one view.

Example:

Name               Time In       Time Out      ETA
Johnnys Supplies    8:30AM        9:00AM      10:15AM
Franks Company      8:45AM        9:05AM      9:45AM
Another Inc.        10:12AM       11:00AM     12:04PM

All of the data would be read in with JSON/PHP.

I need it to work like a tableview where a user can scroll vertically, and select an index. Once that index is selected, the user can click a button to run additional queries based on the data in the selected cell (etc.).

What is the easiest way to implement this? There has to be a way xcode allows you to do this natively? Am I missing something?

All coding examples welcomed!

I have found two options but both require licensing fees:

http://www.ioscomponents.com/Home/IOSDataGrid <-- $400

http://www.binpress.com/app/ios-data-grid-table-view/586 <-- $130

Anyone familiar with these components?

4
  • 2
    If you subclass UITableViewCell, you could write your own really easily. Commented Mar 19, 2015 at 19:17
  • Ask yourself how long you will need to implement it yourself and multiply by your hourly rate. If greater than $130 or $400, buy the component. ;-) Commented Mar 19, 2015 at 19:20
  • 1
    Use UICollectionView. Reference example: appcoda.com/ios-programming-uicollectionview-tutorial Commented Mar 19, 2015 at 19:24
  • I think your question is somewhat ambiguous. From the looks of your example I'd say you'll want these columns to all scroll as one, because each row looks to be a 'unit' of data, so I'd follow DuncanC's advice below and use UITableView with a custom cell which lays out the data in columns. However for other uses where less coupling is required in the rows UIcollectionView might be more appropriate, or indeed UIPickerView, which is actually built from a number of independent UITableViews, so some might see that as the answer to your question. :) Commented Mar 19, 2015 at 19:56

3 Answers 3

15

What's the difference between two columns and two labels next to each other? A divider?

enter image description here

Is this a multi column table view?

Because it's a tableview with ordinary UITableViewCell that have 3 UILabels and 2 UIViews. The views pretend to be dividers by being 1 px wide.

Code should be self explanatory.

.h

@interface MultiColumnTableViewCell : UITableViewCell
@property (strong, nonatomic) UILabel *label1;
@property (strong, nonatomic) UILabel *label2;
@property (strong, nonatomic) UILabel *label3;
@end

.m

@interface MultiColumnTableViewCell ()
@property (strong, nonatomic) UIView *divider1;
@property (strong, nonatomic) UIView *divider2;
@end

@implementation MultiColumnTableViewCell

- (UILabel *)label {
    UILabel *label = [[UILabel alloc] init];
    label.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:label];
    return label;
}

- (UIView *)divider {
    UIView *view = [[UIView alloc] init];
    view.translatesAutoresizingMaskIntoConstraints = NO;
    [view addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:1.0/[[UIScreen mainScreen] scale]]];
    view.backgroundColor = [UIColor lightGrayColor];
    [self.contentView addSubview:view];
    return view;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    self.separatorInset = UIEdgeInsetsZero;
    self.layoutMargins = UIEdgeInsetsZero;
    self.preservesSuperviewLayoutMargins = NO;

    self.divider1 = [self divider];
    self.divider2 = [self divider];

    self.label1 = [self label];
    self.label2 = [self label];
    self.label3 = [self label];

    NSDictionary *views = NSDictionaryOfVariableBindings(_label1, _label2, _label3, _divider1, _divider2);

    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[_label1]-2-[_divider1]-2-[_label2(==_label1)]-2-[_divider2]-2-[_label3(==_label1)]-5-|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:views];
    [self.contentView addConstraints:constraints];

    NSArray *horizontalConstraints1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_divider1]|" options:0 metrics:nil views:views];
    [self.contentView addConstraints:horizontalConstraints1];
    NSArray *horizontalConstraints2 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_divider2]|" options:0 metrics:nil views:views];
    [self.contentView addConstraints:horizontalConstraints2];

    return self;
}

@end

TableViewController:

@implementation MasterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[MultiColumnTableViewCell class] forCellReuseIdentifier:@"Cell"];
    self.tableView.separatorColor = [UIColor lightGrayColor];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MultiColumnTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.label1.text = [NSString stringWithFormat:@"Name %ld", (long)indexPath.row];
    cell.label2.text = [NSString stringWithFormat:@"Start %ld", (long)indexPath.row];
    cell.label3.text = [NSString stringWithFormat:@"End %ld", (long)indexPath.row];
    return cell;
}

@end
Sign up to request clarification or add additional context in comments.

Comments

14

iOS table views are always a single column. On Mac OS you can create what you are after directly.

That said, you could create custom table view cells that display the content you want. In fact it would be quite easy. All you would do is to subclass UITableViewCell and define views (probably UILabels) for each of you columns, then wire them up as outlet properties in the cell. Then rig your table view to register that cell class for the cell identifier you use.

You then write your cellForRowAtIndexPath method to install your data into the different outlet properties.

You could also use a UICollectionView, but it looks to me like a table view with custom cells is a better fit for this application.

4 Comments

Are you able to provide some coding examples? I have never seen this question properly answered on stackoverflow and would be great to put an end to this question.
@FilipKorngut Look at the "Table View Programming Guide for iOS" and look at the sample apps referenced in the docs for UITableView. There are plenty of examples showing how to create a custom cell with multiple labels.
What @rmaddy said. This is well covered by Apple, and in dozens and dozens of sample apps.
This worked perfectly. I found an excellent sample coding video of this method here... youtube.com/watch?v=FfZGKx4BYVU
5

Use UICollectionView, check Apple WWDC 2012 sessions

  • 205 Introducing Collection Views
  • 219 Advanced Collection Views and Building Custom Layouts

from https://developer.apple.com/videos/wwdc/2012/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.