dynamic的UITableView高度

我想设置UITableView以匹配表视图中所有内容的高度。

这是我的故事板

在这里输入图像说明

这个问题是顶部和底部的ImageView在屏幕上始终是静态的。

在这里输入图像说明

假设在桌子视图上有10个项目,但由于屏幕尺寸的限制,只有7个项目显示出来。 我想在用户能够看到底部ImageView之前显示全部10个。 (顺便说一句,所有3的意见,即图像意见和tableview是在uiscrollview)

理想

在这里输入图像说明

我必须处理的一些其他限制是,表格视图中的项目数量是dynamic的,意味着它可以是通常小于10的任何数量,我将稍后从api中检索。 细胞高度也是dynamic的,取决于内容。

我只是刚刚开始一些简单的代码

class ExampleViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var tableView: UITableView! var items: [String] = [ "Item 01", "Item 02", "Item 03", "Item 04", "Item 05", "Item 06", "Item 07", "Item 08", "Item 09", "Item 10"] override func viewDidLoad() { super.viewDidLoad() self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.items.count; } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell cell.textLabel?.text = self.items[indexPath.row] return cell } } 

你需要设置一个IBOutlet到设置tableView高度的NSLayoutConstraint (首先你需要用任何值创build高度约束,无所谓),然后按住Ctrl键拖动到你的类文件

在这里输入图像说明

然后在你的viewWillAppear你必须计算tableView高度,并设置它。 喜欢这个:

 var tableViewHeight:CGFloat = 0; for (var i = tableView(self.tableView , numberOfRowsInSection: 0) - 1; i>0; i-=1 ){ tableViewHeight = height + tableView(self.tableView, heightForRowAtIndexPath: NSIndexPath(forRow: i, inSection: 0) ) } tableViewHeightLayout.constant = tableViewHeight 

而这是非常多的。 这将给你的scrollView内容大小,不应该提出任何警告。

  1. 子类化你的UITableView来覆盖intrinsicContentSize作为它的contentSize,像这样:

     override var intrinsicContentSize: CGSize { return contentSize } 
  2. 然后使用自动行高为你的表,所以你的exampleViewController的viewDidLoad将具有:

     tableView.estimatedRowHeight = 44 

    和UITableViewDelegate函数:

     func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableViewAutomaticDimension } 
  3. 当您从API接收数据并重新载入表格时,请致电:

     tableView.invalidateIntrinsicContentSize() 

    这将告诉你的表格调整自己的大小,因为它的内容相同(因为覆盖),并根据需要移动你的底部图像。


如果故事板引发错误,说明UIScrollView的UITableView高度不明确,请selectUITableView并在“大小”检查器中为其指定占位符内部大小。

在这种情况下,不要使底部单元格为静态,使其成为表视图的一部分,并使用表视图委托方法在最后一行中插入底部图像 – insertRowAtIndexPath

在这种情况下,在表格页脚视图中添加底部的imageView(红色)。

要在UITableView添加页脚视图,你可以使用:

 tableViewObj.tableFooterView = footerViewObj; 

您可能必须实现表视图内在的内容大小。 请检查这个答案 ,看看是否有帮助。

我记得有这个问题,甚至创build了一个自定义的UITableView子类。

 #import "IntrinsicTableView.h" @implementation IntrinsicTableView #pragma mark - UIView - (CGSize)intrinsicContentSize { return CGSizeMake(UIViewNoIntrinsicMetric, self.contentSize.height); } #pragma mark - UITableView - (void)endUpdates { [super endUpdates]; [self invalidateIntrinsicContentSize]; } - (void)reloadData { [super reloadData]; [self invalidateIntrinsicContentSize]; } - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation { [super reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation]; [self invalidateIntrinsicContentSize]; } - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation { [super reloadSections:sections withRowAnimation:animation]; [self invalidateIntrinsicContentSize]; } - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation { [super insertRowsAtIndexPaths:indexPaths withRowAnimation:animation]; [self invalidateIntrinsicContentSize]; } - (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation { [super insertSections:sections withRowAnimation:animation]; [self invalidateIntrinsicContentSize]; } - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation { [super deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation]; [self invalidateIntrinsicContentSize]; } - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation { [super deleteSections:sections withRowAnimation:animation]; [self invalidateIntrinsicContentSize]; } @end 

试试这也

 in ViewDidLoad self.table.estimatedRowHeight = 44.0 ; self.table.rowHeight = UITableViewAutomaticDimension; 

索引path行的高度

 -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewAutomaticDimension;}