【问题标题】:How to make a-two-column UITableViewController list?如何制作一个两列 UITableViewController 列表?
【发布时间】:2011-09-12 07:27:06
【问题描述】:

我需要用两列列表显示我的表格视图。我阅读了一些有关通过覆盖 drawRect (here) 来制作一些网格的相关文章。但是,我正在寻找一种简单的方法来设计我的单元格,在笔尖中使用 IB,然后加载它并在每行上推送两个单元格。 drawRect 的示例不合适,因为它涉及手动设置位置。我只需要用一些自动调整大小来推动这两个单元格,就是这样。是否可以?

我正在寻找类似(在 cellForRowAtIndexPath 中):

cell.contentView = emptyUIViewContainer;
[cell.contentView addSubview:FirstColumnUIView];
[cell.contentView addSubview:SecondColumnUIView];

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

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

- (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:]: unrecognized selector sent to instance”

更新 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,但其他两个都可以。

【问题讨论】:

  • 每一列都需要是一个独立的单元格吗?您可以将子视图添加到普通单元格并独立填充它们吗?您希望在编辑、选择等方面看到什么样的行为?
  • 是的,每一列都应该是独立的单元格项目,我将在排序时使用。作为项目的单元格将包含可通过点击访问的自定义对象(按钮、图像)。问题是我需要一个可旋转的桌子。在纵向模式下,我需要每行显示一列,并且需要在横向模式下显示两列。目前我正在尝试让代码在两个单元格的横向模式下工作。

标签: iphone objective-c uitableview two-columns


【解决方案1】:

您不能(或至少不应该)将两个表格视图并排放置。而且,您已经知道,表格视图只有一列。

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

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

由于表格对于 UIKit 来说如此重要,因此 Apple 的文档集中有很多示例和一些很好的 WWDC 演讲。您可以从 iTunes 下载视频。

【讨论】:

  • 是的,我明白一个表格只能有一列,我可以在一个单元格中放入任意数量的数据。所以,我正在考虑将两个单元格嵌套到 CellContainer 中并从 cellForRowAtIndexPath 返回它。您可以从我的更新代码中看到它。我正在尝试的代码从代码中完成所有事情(没有笔尖),但我仍然无法实现嵌套。我是否使用该代码破坏了单元格构造规则?
  • 既然可以有一个单元格和两个UILabels,为什么还要使用两个UITableViewCells?
  • 另外,为什么每次显示单元格时都添加新的UITableViewCells?!如果你必须这样做(你没有)至少只添加一次子视图。
  • 我需要一个可旋转的桌子。在纵向模式下,我需要每行显示一列,并且需要在横向模式下显示两列。是的,这些单元格没有在我的代码中重用,只是在试验代码。奇怪,但经过几次尝试,代码停止崩溃,但我的所有单元格都没有黑色背景。
【解决方案2】:

希望对你有所帮助...

http://www.iphonedevx.com/?p=153

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

希望对你有所帮助...

【讨论】:

  • 这与我在帖子中提到的涉及 drawRect 的示例相同。并排的两个表格视图不适合我当前的任务:(
猜你喜欢
  • 2017-08-10
  • 2012-11-12
  • 2021-01-08
  • 1970-01-01
  • 2021-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-10
相关资源
最近更新 更多