如何在Xcode中创建具有多个列的UI TableView?

Fil*_*gut 8 objective-c uitableview ios

我正在使用Xcode开发iOS 8应用程序.我需要在一个视图中显示一个包含许多列和行数据的表.

例:

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
Run Code Online (Sandbox Code Playgroud)

所有数据都将使用JSON/PHP读入.

我需要它像tableview一样工作,用户可以垂直滚动,并选择一个索引.选择该索引后,用户可以单击按钮以根据所选单元格中的数据运行其他查询(等).

实现这个的最简单方法是什么?必须有一种方法xcode允许你本地执行此操作?我错过了什么吗?

欢迎所有编码示例!

我找到了两个选项,但都需要许可费:

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

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

有谁熟悉这些组件?

Dun*_*n C 14

iOS表视图始终是单个列.在Mac OS上,您可以直接创建自己的内容.

也就是说,您可以创建显示所需内容的自定义表格视图单元格.实际上这很容易.您要做的就是为UITableViewCell创建子类并为每个列定义视图(可能是UILabels),然后将它们作为单元格中的出口属性连接起来.然后装配表视图以为您使用的单元标识符注册该单元类.

然后编写cellForRowAtIndexPath方法,将数据安装到不同的出口属性中.

您也可以使用UICollectionView,但它看起来像一个带有自定义单元格的表格视图更适合此应用程序.

  • 您能提供一些编码示例吗?我从来没有在stackoverflow上正确回答过这个问题,并且很好地结束了这个问题. (3认同)

Mat*_*uch 13

两列和两个标签之间的区别是什么?分频器?

在此输入图像描述

这是一个多列表视图吗?

因为它是普通的桌面视图UITableViewCell,有3 UILabels和2 UIViews.这些观点假装是1分宽的分隔符.

代码应该是自我解释的.

.H

@interface MultiColumnTableViewCell : UITableViewCell
@property (strong, nonatomic) UILabel *label1;
@property (strong, nonatomic) UILabel *label2;
@property (strong, nonatomic) UILabel *label3;
@end
Run Code Online (Sandbox Code Playgroud)

.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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)


JOM*_*JOM 5

使用 UICollectionView,查看 Apple WWDC 2012 会议

  • 第 205 章
  • 219 个高级集合视图和构建自定义布局

来自https://developer.apple.com/videos/wwdc/2012/