【问题标题】:Handling a tableview with static cells and another searchResultsTableView处理带有静态单元格的表格视图和另一个 searchResultsTableView
【发布时间】:2013-05-26 13:22:23
【问题描述】:

最初,我有一个主表视图控制器,其中包含使用情节提要布置的静态单元格的表视图。随后,我在这个 UITableViewController 中添加了一个 SearchDisplayController。在我的 tableview 数据源委托方法中,例如 numberOfSectionsInTableView: 和 numberOfRowsInSection,我通过检查以下内容来区分我自己的 tableview(带有静态单元格)和搜索显示控制器的 searchResultsTableView:

if (tableView == self.searchDisplayController.searchResultsTableView)
{
   // logic for searchResultsTableView;
}
else
{
  // logic for my main tableView
}

据我所知,这似乎是正确的方法。但是,当我为 cellForRowAtIndexPath 方法尝试以下操作时,由于未捕获的异常“NSInternalInconsistencyException”,我收到消息正在终止应用程序崩溃,原因:“UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:”

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        // Just want to use a default cell. There seems to be no good way of specifying a prototype cell for this in the storyboard.
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if ( cell == nil ) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        return cell;
    }
    else
    {
         // how to handle this case?
         return nil;
    }
}

以前,如果没有搜索显示控制器,我不必实现此方法,因为我的单元格是静态的。我想我的问题是,我应该如何处理混合案例(在 cellForRowAtIndexPath 中以及在同一个故事板中指定两种单元格),其中我有一个带有动态单元格的搜索显示控制器 tableview 和一个带有静态单元格的 tableview?我想这种情况并不少见。

提前致谢!

编辑:

虽然按照第一个评论者的建议在 else 子句中调用 super 方法似乎可以修复崩溃,但我遇到了另一个问题,我觉得这是由于 tableViewController 作为静态 tableview 的代表的固有问题和一个非静态的(搜索显示结果表视图)。

新问题:我的静态表格视图有 2 个静态单元格。当我用超过 2 行填充我的搜索结果表视图时,我得到一个 NSRangeException',原因:-[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 1]。

似乎 searchResultsTableView 以某种方式从我的主静态 tableview 派生了行数(当我添加第三个静态单元格时结果是一致的),即使委托方法:numberOfRowsInSection 被触发为 searchResultsTableView 的情况,并返回正确的数字。

使静态表格视图与搜索显示表格视图配合得很好的任何变通方法?我正在考虑将我的主要静态表格视图转换为带有动态单元格的表格视图。欢迎提出其他建议,谢谢!

【问题讨论】:

  • 我不确定,但你试过return [super tableView:tableView cellForRowAtIndexPath:indexPath]吗?
  • 您的操作方式与 tableView == self.searchDisplayController.searchResultsTableView 返回 true 时的方式相同。您需要在 IB 中或以编程方式自己创建一些单元格,然后根据 indexPath 创建并返回适合您需要的单元格。
  • 调用 super 方法有效。谢谢!
  • 我不确定为什么调用 super 会起作用,但您根本不应该为静态表视图实现表视图数据源方法(它们的单元格应该通过 IBOutlets 填充)。我认为正确的方法是一起去掉 else 子句。
  • @rdelmar,你可能有一点。超级方法解决了最初的崩溃,但我遇到了新问题,我怀疑这可能是由于 tableviewcontroller 是静态 tableview 和非静态 tableview 的代表。我已经用新问题更新了我的问题。无论如何,cellForRowAtIndexPath 必须返回一个 UITableViewCell,所以我不确定如何摆脱 else 子句。嗯还有其他想法吗?

标签: iphone ios objective-c uitableview ios5


【解决方案1】:

最后我的解决方法是将静态表格视图转换为具有动态单元格的表格视图。它不漂亮,但很有效。

【讨论】:

    【解决方案2】:

    我也遇到过这个问题。所以我的决定是创建一个带有协议的类,然后将它作为 DataSource 分配给 UISearchDisplayViewController。

    【讨论】:

      【解决方案3】:

      您无需将静态单元格更改为动态单元格。您可以执行以下操作:

      if (tableView == self.searchDisplayController.searchResultsTableView)
      {
         // logic for searchResultsTableView;
      }
      else
      {
         UITableViewCell *cell = [super tableView:tableView
                             cellForRowAtIndexPath:indexPath];
         return cell;
      
      }
      

      【讨论】:

        【解决方案4】:
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            if (tableView == self.searchDisplayController.searchResultsTableView)
            {
                // Just want to use a default cell. There seems to be no good way of specifying a prototype cell for this in the storyboard.
                static NSString *CellIdentifier = @"Cell";
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                if ( cell == nil ) {
                    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
                }
                return cell;
            }
            else
            {
                  UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
                 return cell;
            }
        }
        

        这可以很好地解决您的问题。

        【讨论】:

          猜你喜欢
          • 2012-10-03
          • 1970-01-01
          • 2014-09-02
          • 1970-01-01
          • 2018-10-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多