Iphone 如何制作两列UITableViewController列表?

Iphone 如何制作两列UITableViewController列表?,iphone,objective-c,uitableview,two-columns,Iphone,Objective C,Uitableview,Two Columns,我需要为我的表视图提供一个两列列表。我读了一些有关通过重写drawRect()生成一些网格的相关文章。然而,我正在寻找一种简单的方法,在nib中使用IB设计我的单元,然后加载它并在每行上推两个单元。drawRect的示例不适用,因为它涉及手动设置位置。我只需要通过自动调整大小来推动这两个单元格,就这样。可能吗 我正在寻找类似(在CellForRowatineXpath中)的内容: 我不需要为这两列设置两个单独的NIB,因为每一列的格式都是相同的,只是包含一些其他数据。有什么想法吗 更新:直觉上,

我需要为我的表视图提供一个两列列表。我读了一些有关通过重写drawRect()生成一些网格的相关文章。然而,我正在寻找一种简单的方法,在nib中使用IB设计我的单元,然后加载它并在每行上推两个单元。drawRect的示例不适用,因为它涉及手动设置位置。我只需要通过自动调整大小来推动这两个单元格,就这样。可能吗

我正在寻找类似(在CellForRowatineXpath中)的内容:

我不需要为这两列设置两个单独的NIB,因为每一列的格式都是相同的,只是包含一些其他数据。有什么想法吗

更新:直觉上,我正在尝试这样做:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

  static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell1 == nil) {
    cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  }
  // Configure the first cell.
  cell1.textLabel.text = ...some text

  // Configure the second cell
  UITableViewCell *cell2 = [[UITableViewCell alloc] init];
  cell2.textLabel.text = ...some text

  //set row as container for two cells    
  UITableViewCell *twoColumnRowView = [[UIView alloc] init]; //initWithFrame:CGRectMake(0, 0, 200, 20)];

  cell1.contentView.frame = CGRectMake(0, 0, 100, 20);
  [twoColumnRowView addSubview:cell1];
  cell2.contentView.frame = CGRectMake(100, 0, 100, 20);
  [twoColumnRowView addSubview:cell2];

  return twoColumnRowView; // cell;
}
这只是我正在玩的一个原始原型。但代码在运行时崩溃,“由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'-[UIView setTableViewStyle:]:无法识别的选择器发送到实例”

更新2。我修改了一个代码,使它看起来更实用。奇怪的是,经过几次尝试,我的应用程序运行正常,没有崩溃,但所有单元格都有奇怪的黑色背景。代码如下:

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

  // initialize first cell
  UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:FirstCellIdentifier];

  if (cell1 == nil) {
    cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstCellIdentifier] autorelease];
  }

  //initialize second cell
  UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:SecondCellIdentifier];

  if (cell2 == nil) {
    cell2 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SecondCellIdentifier] autorelease];
  }

  cell1.textLabel.text = ...data
  cell2.textLabel.text = ...data

  UITableViewCell *twoColumnRowView = [[UITableViewCell alloc] init];

  [twoColumnRowView addSubview:cell1];
  //cell2.contentView.frame = CGRectMake(100, 0, 100, 20);
  [twoColumnRowView addSubview:cell2];

  return twoColumnRowView; // cell;
}

我没有重复使用twoColumnRowView,但其他两个都重复使用。

希望这能帮助您

否则,如果需要独立滚动,最好的方法是将两个表视图并排放置


希望这能帮助您……

您不能(或至少不应该)将两个表视图放在一起。而且,正如您已经知道的,表视图只有一个列

但是,您可以在每个单元格中放入任意数量的数据

您可以在Interface Builder中完成大部分操作。使用
UITableViewCell
创建一个XIB。将两个
UILabel
s拖放到正确的位置。您可以通过使用
viewWithTag:
方法查找标签来更新标签,但最好创建一个自定义的
UITableViewCell
类,其属性指向每个标签


由于表格对于UIKit来说非常重要,所以在苹果的文档集中有很多示例和一些很好的WWDC对话。您可以从iTunes下载视频。

这与我在帖子中提到的drawRect相同。并列的两个表视图不适合我当前的任务:(每列是否需要是一个独立的单元格?能否将子视图添加到一个普通单元格并独立填充它们?您希望在编辑、选择等时看到什么样的行为?是的,每列都应作为独立的单元格项,我将在排序时使用。单元格项将包含自定义对象(按钮、图像)这将可以通过点击访问。问题是我需要有一个可旋转的表。在纵向模式下,我需要每行显示一列,在横向模式下需要显示两列。目前,我正在尝试让代码在横向模式下使用两个单元格。是的,我知道一个表只能有一列,我可以放置尽可能多的数据在我想要的单元格中。因此,我正在考虑将两个单元格嵌套到CellContainer中,并从CellForRowatineXpath返回它。您可以从我更新的代码中看到它。我尝试的代码从代码中执行所有操作(无nib)但我仍然无法实现这种嵌套。我是否违反了使用该代码构建单元格的规则?如果可以有一个单元格和两个
UILabel
s,为什么要使用两个
UITableViewCell
s?此外,为什么每次显示单元格时都要添加新的
UITableViewCell
s?如果必须这样做(你没有这样做)至少只添加一次子视图。我需要一个可旋转的表。在纵向模式下,我需要每行显示一列,在横向模式下,我需要显示两列。是的,这些单元格在我的代码中没有重用,只是在试验代码。奇怪的是,尝试了几次后,代码停止崩溃,但我的所有单元格都在黑色背景中没有颜色。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *FirstCellIdentifier = @"FirstCellIdentifier"; 
  static NSString *SecondCellIdentifier = @"SecondCellIdentifier";

  // initialize first cell
  UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:FirstCellIdentifier];

  if (cell1 == nil) {
    cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstCellIdentifier] autorelease];
  }

  //initialize second cell
  UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:SecondCellIdentifier];

  if (cell2 == nil) {
    cell2 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SecondCellIdentifier] autorelease];
  }

  cell1.textLabel.text = ...data
  cell2.textLabel.text = ...data

  UITableViewCell *twoColumnRowView = [[UITableViewCell alloc] init];

  [twoColumnRowView addSubview:cell1];
  //cell2.contentView.frame = CGRectMake(100, 0, 100, 20);
  [twoColumnRowView addSubview:cell2];

  return twoColumnRowView; // cell;
}