iOS Tableview中的多列 [英] Multiple columns in iOS Tableview

查看:379
本文介绍了iOS Tableview中的多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to create multiple columns in row of the tableview?

推荐答案

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

UITableView isn't really designed for multiple columns. But you can simulate columns by creating a custom UITableCell class. Build your custom cell in Interface Builder, adding elements for each column. Give each element a tag so you can reference it in your controller.

为您的控制器提供一个插座,以从笔尖加载电池:

Give your controller an outlet to load the cell from your nib:

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

然后,在表视图委托的cellForRowAtIndexPath方法中,按标签分配这些值.

Then, in your table view delegate's cellForRowAtIndexPath method, assign those values by tag.

-(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 Tableview中的多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆