如何创建多行多列的tableview? [英] How do I create a tableview with multiple rows and multiple columns?

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

问题描述

我知道如何创建一个单列多行的 tableview,但我不知道如何创建一个多行多列的 tableview.

I know how to create a tableview with a single column and multiple rows, but I don't know how to create a tableview with multiple rows and multiple columns.

有人可以帮我吗?

推荐答案

-(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;
}

我认为这段代码会帮助你 UITableView 并不是真正为多列设计的.但是您可以通过创建自定义 UITableCell 类来模拟列.在 Interface Builder 中构建自定义单元格,为每列添加元素.给每个元素一个标签,这样你就可以在你的控制器中引用它.

i think this code will help you 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.

这篇关于如何创建多行多列的tableview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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