ios - iOS Tableview 中的多列

标签 ios objective-c uitableview

如何在tableview的行中创建多个列?

最佳答案

UITableView 并不是真正为多列设计的。但是您可以通过创建自定义 UITableCell 类来模拟列。在 Interface Builder 中构建自定义单元格,为每一列添加元素。给每个元素一个标签,这样你就可以在你的 Controller 中引用它。

给你的 Controller 一个 socket 来从你的 Nib 加载电池:

@property(nonatomic,retain)IBOutlet UITableViewCell *myCell;

然后,在您的 TableView 委托(delegate)的 cellForRowAtIndexPath 方法中,通过标记分配这些值。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *cellIdentifier = @"MyCell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  if (cell == nil) {
    // load cell from nib to controller's IBOutlet
    [[NSBundle mainBundle] loadNibNamed:@"MyTableCellView" owner:self options:nil];
    // assign IBOutlet to cell
    cell = myCell;
    self.myCell = nil;
  }

  id modelObject = [myModel objectAtIndex:[indexPath.row]];

  UILabel *label;
  label = (UILabel *)[cell viewWithTag:1];
  label.text = [modelObject firstField];

  label = (UILabel *)[cell viewWithTag:2];
  label.text = [modelObject secondField];

  label = (UILabel *)[cell viewWithTag:3];
  label.text = [modelObject thirdField];

  return cell;
}

关于ios - iOS Tableview 中的多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3252682/

相关文章:

iphone - 如何在iPhone的手机上显示倒数计时器?

iphone - iAd 出现和消失

ios - 如何在一个类中使用不同的委托(delegate)和数据源方法实现两个 Tableview?

ios - 自定义 UITableViewController 问题

ios - 使用前置摄像头进行手势检测

ios - 如何制作 UIPopoverController (Swift)?

iphone - 当应用程序重新启动时,如何保留 UITextField 中存储在 plist 中的数据?

ios - 数组过滤器与核心数据拉取性能

iphone - 显示带有视频和链接的 PDF

iphone - Facebook iOs SDK - 在用户的墙上发布预填信息