【问题标题】:UISearchDisplayController changing row heightUISearchDisplayController 改变行高
【发布时间】:2010-04-14 17:51:38
【问题描述】:

我在 Interface Builder 中将我的 UITableView 行高设置为 54.0。我在那个观点上有一个UISearchDisplayController。当用户点击其中的搜索栏时,表格会正确调整大小。但是,当他们开始输入(并实际进行搜索)时,行高会减小。在搜索点击 Cancel 之前,它一直是错误的。

我在 Apple 的网站上找不到有关此行为的文档。

我尝试在UISearchDisplayDelegate 委托调用中设置行高。这可能是正确的方法,但我不知道细节,无法让它发挥作用。

我也尝试过实现- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;。这行得通,但我在此列表中有 数千 个条目,无法承受性能损失。

解决这个问题的正确方法是什么?

【问题讨论】:

  • 我鼓励大家看看下面 steven 的回答。他的唯一答案是 a) 遵循 UITableViewDataSource 协议,将 UISearchTableView 视为任何其他表,并且 b) 支持多个行的高度。如果您具有显示自定义“无搜索结果”单元格的标准功能,该单元格通常会调整大小以占用整个 tableView 框架,则 B 非常重要。
  • 我怀疑这是真的。我发现在这种情况下实现heightForRowAtIndexPath 对我有用,这对我的表(有数万行)来说很慢。确保您没有仅针对某些 tableView 参数返回可变高度,您应该没问题。
  • heightForRowAtIndexPath 永远不会对tableView 的性能产生重大影响。无论你有 10 行还是 10,000 行,tableView 只处理当前可见的行。因此,除非您花费大量时间来确定行高应该是多少,否则您的 tableView 应该能够在滚动时保持 60fps 的动画。
  • 这不是真的。根据 Apple 的文档:“每次显示表视图时,它都会在其每一行的委托上调用 tableView:heightForRowAtIndexPath:,这可能会导致具有大量行(大约 1000 或更多)的表视图出现严重的性能问题)。”

标签: iphone uitableview uisearchdisplaycontroller


【解决方案1】:

执行此操作的正确方法是使用以下委托。

- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
    tableView.rowHeight = 54.0f; // or some other height
}

在创建或显示UITableView 时调用它。

如果你在不进行搜索的时候碰巧在其他代码中调用了searchDisplayController.searchResultsTableView,它会提前创建UITableView。如果您使用willShow委托方法设置rowHeight,如果事先创建了tableView,它将错过更改rowHeight的时机(由于某种原因)。

【讨论】:

  • 我不得不在此处移动已接受的答案多少次让我感到尴尬,但您显然是正确的。谢谢。
【解决方案2】:

我找到了!

假设表存储在tableView:

- (void)viewDidLoad;
{
    [super viewDidLoad];
    self.searchDisplayController.searchResultsTableView.rowHeight = tableView.rowHeight;
}

没有其他必要了。

编辑:查看 netmikey 的答案以获得更好的解决方案。

【讨论】:

    【解决方案3】:

    史蒂夫,您的解决方案对我不起作用:第一次搜索结果显示正常,但是当用户点击“取消”(关闭搜索栏)然后重新打开它并在其中输入搜索词时它,高度又错了。

    DigDog 使用searchDisplayController:didShowSearchResultsTableView 的解决方案有点工作,但用户在搜索开始时看到单元格高度“跳跃”。

    我找到的解决这两个问题的解决方案是使用:

    - (void)searchDisplayController: (UISearchDisplayController *)controller 
     willShowSearchResultsTableView: (UITableView *)searchTableView {
    
        searchTableView.rowHeight = myTableView.rowHeight;
    }
    

    ...在UISearchDisplayDelegate.

    问候, 网络密钥

    【讨论】:

    • 这是一个更好的答案。公认。 :)
    【解决方案4】:

    在 viewDidLoad 中:

    您可以将搜索显示控制器的委托设置为 self:

    - (void)viewDidLoad 
    {
        self.searchDisplayController.delegate = self;
    }
    

    然后,在视图控制器中实现表视图委托:

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (tableview == self.searchDisplayController.tableView) {
            return 55;
        } else if (tableView == self.tableView) {
           return  44;
         }
    }
    

    【讨论】:

    • 我不能 +1 够了。这是 a) 遵循UITableViewDataSource 协议的唯一答案,将UISearchTableView 视为任何其他表,并且 b) 支持多个行的高度。如果您具有显示自定义“无搜索结果”单元格的标准功能,该单元格的大小通常会占用整个 tableView 框架,则 B 非常重要。
    【解决方案5】:

    重写方法 didLoadSearchResultsTableView 应该可以解决问题。

    - (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView {
        tableView.rowHeight = 82.0; // Change height of search result row
    }
    

    更多解释来自this blog

    【讨论】:

      【解决方案6】:

      两个都需要设置

      - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {    
          return 54.;
      }
      

      self.tableView.rowHeight = 54.;
      

      也在你的 UISearchDisplayDelegate 中

      - (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView {
          tableView.rowHeight = 54.; 
      }
      

      否则,当搜索出现“无结果”时,单元格高度将回退到默认值。

      【讨论】:

      • 实际上,heightForRowAtIndexPath: 和 .rowHeight 的行为略有不同(至少在您的情况下),因此请确保同时设置它们,而不仅仅是其中一个。
      • 我不能使用 heightForRowAtIndexPath。有 20,000 行,它在真实设备上会大大降低表格的速度。
      【解决方案7】:

      我一直在使用 ios 7.0 和 Xcode 5.0。我认为您应该在此方法中更改行高:

          - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
          {
          // Return the number of rows in the section.
          if (tableView == self.searchDisplayController.searchResultsTableView)
          {
              tableView.rowHeight = ...;// change your row height here!
              return [self.searchResults count];
          }
          else
          {
              ...
              return ...;
          }
          }
      

      因为每次tableview试图显示它的布局时,都会调用这个方法,所以每次在搜索栏输入一个字母,都会调用这个方法,并且行高不会有机会改变。

      【讨论】:

        【解决方案8】:

        没有一个解决方案对我有用...

        我自己进行了尝试和尝试...并使其正常工作...

        如果您希望普通和搜索表视图具有不同的行高...

        -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        // for normal results
             if (tableView==self.tableView) return 54;
        // for search results
             else return 54; }
        

        如果你想要共同的身高......

        -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
             return 54; }
        

        PS:请记住,如果每行的行高相同,则此方法在资源使用方面不是很有效,因为会为每个单元格重新计算行高...虽然,这在大多数情况下,我仍在寻找更好的解决方案!

        【讨论】:

          猜你喜欢
          • 2013-12-28
          • 1970-01-01
          • 2021-08-24
          • 1970-01-01
          • 1970-01-01
          • 2018-01-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多