【问题标题】:How do I do the UISearchDisplayController's tableView to use the prototype cell defined for the regular tableViewUISearchDisplayController 的 tableView 如何使用为常规 tableView 定义的原型单元格
【发布时间】:2014-12-06 03:06:33
【问题描述】:

我有一个带有搜索栏和搜索显示控制器的 tableViewController。

主 tableViewController 的 tableView 有一个原型单元定义在故事板上。

我在生产线上遇到了崩溃

   FileManagerTableViewCell *cell = (FileManagerTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

当搜索tableview即将显示时。显然,搜索 tableView 无法访问故事板上主 tableView 上定义的原型单元格。

如何让搜索表视图访问为情节提要上的主表视图定义的原型单元格?

【问题讨论】:

    标签: ios uitableview storyboard tableview


    【解决方案1】:

    如果您希望两个表格使用相同的单元格,您应该在 xib 文件中而不是在情节提要中设计该单元格。让两个表都注册笔尖,使用 registerNib:forCellReuseIdentifier:.

    【讨论】:

    • 谢谢,但我发现了如何做到这一点以及问题所在。看我的回答。还是谢谢。
    【解决方案2】:

    这个问题的答案很容易编辑,但很难理解为什么 Apple 创造了这样半熟的东西。解决方法来自this answer的第一条评论

    答案是使用 self.tableview 而不是 tableview

       FileManagerTableViewCell *cell = (FileManagerTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    

    解释:当搜索开始时,iOS切换显示搜索tableview但tableView没有原型单元格,所以这一行

       FileManagerTableViewCell *cell = (FileManagerTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    

    要求 iOS 从搜索 tableView 中取出一个单元格。通过使用 self.tableview,您可以对 dequeue 说来自主 tableView 的一个单元格,其中包含在故事板上定义的原型单元格。

    我希望有一天 iOS API 可以让我们的生活变得轻松,而这在今天还远未发生。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-23
      • 1970-01-01
      • 2018-03-08
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多