UISearchDisplayController使用AutoLayout约束的表格单元格渲染

UISearchDisplayController使用AutoLayout约束的表格单元格渲染

问题描述:

我在iOS 7 & 8应用程序中实现了UISearchDisplayController。我使用Storyboard来构建界面,并且我已经成功地将搜索控制器添加到了我的表中,并且一切都很好,只要搜索工作正常。UISearchDisplayController使用AutoLayout约束的表格单元格渲染

我的问题是,表格行已配置使用自动布局,以提供基于单元格内容的动态行高。虽然常规表格中的布局正常,但这些约束不会应用于搜索结果表格中。这里有一些漂亮的图片来展示我的意思。

Left: Normal table view; Right: Search Results table

每个教程和文件的位假定结果表将有一个静态的高度,建议是使用tableview.rowHeighttableView:heightForRowAtIndexPath返回浮动。

cellForRowAtIndexPath方法中,我出列我的手机从普通实现代码如下所示:

ScheduleTableViewCell * cell = [self.scheduleTable dequeueReusableCellWithIdentifier:@"cell"]; 

我本来期望这对使用电池作为我在故事板他们摆放好了,但是这显然不包括这些限制。

是否有一些方法可以使这发生?或者我需要像AutoLayout之前那样手动确定每行的高度?像某种肮脏的动物?

+0

您是否在故事板中为常规表格和搜索结果表格使用了您创建的单元格? – rdelmar 2014-10-02 18:48:42

+0

是的,这就是我想要做的。 – 2014-10-02 18:52:37

+0

您是否使用iOS 8中引入的新自定义单元格(如将表格视图的估计行高和rowHeight设置为UITableViewAutomaticDimension)?如果是这样,你是否已经为两个表做了这些? – rdelmar 2014-10-02 19:00:32

当使用自调整单元格时,应将两个表的rowHeight属性设置为UITableViewAutomaticDimension,并为两个表都设置estimatedRowHeight。你可以在viewDidLoad中做到这一点,不需要实现heightForRowAtIndexPath。

我挣扎了这个最近几天也不幸rdelmar的回答并没有解决这个问题对我来说,所以这里是我必须做的来解决这个问题:

相反的:

self.searchDisplayController.searchResultsTableView.rowHeight = UITableViewAutomaticDimension; 

我不得不使用:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    return UITableViewAutomaticDimension; 
} 

这只是让我走了一半。我在tableViewCell中的约束也存在一个潜在的问题。我的textView需要动态高度,所以我没有为textView提供任何高度限制。它根据它对上下视图的约束来适当调整大小。这在我的原始tableView中效果很好,但在searchResultsTableView中并不如此。

我不得不将高度约束添加到textView并使用> =(大于或等于)常量。最后,searchResultsTableView模仿我的原始tableView。

+0

Frankie,我为我的tableview设置了高度约束(> =就像你提到的那样),但现在不是扩展,uilabel被切断了...... – cableload 2015-10-08 03:04:07

根据@ rdelmar的回答我想补充。对于像我这样感到困惑的人群,您应该设置以下两个属性:

// for the main tableView 
self.tableView.rowHeight = UITableViewAutomaticDimension 
self.tableView.estimatedRowHeight = 100 //or whatever height you prefer 

// and for searchDisplayController 
self.searchDisplayController?.searchResultsTableView.rowHeight = UITableViewAutomaticDimension 
self.searchDisplayController?.searchResultsTableView.estimatedRowHeight = 100 //or whatever height you prefer 

P.S.对不起,我不能添加评论,所以我正在写一个答案