iOS Tableview中的多列

sri*_*sri 4 objective-c uitableview ios

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

Chr*_*lay 6

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

给你的控制器一个插座,从你的笔尖加载单元格:

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

然后,在表视图委托的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;
}
Run Code Online (Sandbox Code Playgroud)