动态UITableView高度 [英] Dynamic UITableView height

查看:93
本文介绍了动态UITableView高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将UITableView设置为与表格视图中所有内容的高度相匹配.

I would like to set the UITableView to match the height for all the contents in the table view.

这是我的故事板

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

The problem with this is the top and bottom ImageView is always static on the screen.

假设在表格视图中有10个项目,但由于屏幕尺寸限制,仅显示7个项目.我想在用户能够看到底部ImageView之前显示全部10个. (顺便说一下,所有三个视图,即图像视图和表视图都在u​​iscrollview中)

The there are suppose to be 10 items on the table view but only 7 shows up due to screen size limitation. I would like to show all 10 before user is able to see the bottom ImageView. (btw, all 3 of the views ie. both the image views and tableview is in a uiscrollview)

理想

我需要处理的其他一些限制是表视图中的项目数是动态的,这意味着它的数量通常可以少于10,以后我会从api中检索到该数量.单元格的高度也是动态的,具体取决于内容.

Some of the other limitations that i have to work with is that the number of items in the table view is dynamic meaning it can be in any amount of usually less than 10 that i will later retrieve from an api. And the cell height is also dynamic depending on the contents.

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

I have only just started with some simple code

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(首先,您需要使用任何值创建高度约束,问题),然后按住Ctrl并将其拖到您的课程文件中

You need to set an IBOutlet to the NSLayoutConstraint that sets the tableView height (first you need create the height constraint with any value, doesn't matter) and then ctrl drag it to your class file

然后在您的viewWillAppear中,您必须计算并设置tableView高度.像这样:

Then in your viewWillAppear you have to calculate the tableView height and set it. Like this:

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内容大小,并且不应该发出任何警告.

And that's pretty much it. That will give your scrollView content size and shouldn't raise any warnings.

这篇关于动态UITableView高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆