如果行高为UITableViewAutomaticDimension,则UITableViewWrapperView大小不会随着内容而变化

时间:2022-08-24 07:34:59

UITableViewWrapperView size is not changing as per the content if row height is UITableViewAutomaticDimension.

如果行高为UITableViewAutomaticDimension,则UITableViewWrapperView大小不会随着内容而变化。

I am trying to create a Feed listing with TableView and dynamic height rows. TableView is scrollable but UITableViewWrapperView looks like it's not changing its size!

我正在尝试创建一个包含TableView和动态高度行的提要列表。TableView是可滚动的,但是UITableViewWrapperView看起来并没有改变它的大小!

Below is the code and screenshot of the view hierarchy.

下面是视图层次结构的代码和屏幕截图。

I think its strange.

我认为它很奇怪。

Code:

代码:

class FeedController: UITableViewController {

    var items: NSMutableArray = []

    override func viewDidLoad() {
        super.viewDidLoad()

        refreshControl?.addTarget(self, action: "feedLoad", forControlEvents: UIControlEvents.ValueChanged)

        self.feedLoad()

    }

    override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 120.0
    }

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        println("called")
        return UITableViewAutomaticDimension
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.items.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        return self.getCellForItemAtIndexPath( indexPath )
    }

    func getCellForItemAtIndexPath(indexPath: NSIndexPath) -> UITableViewCell {

        let item: Dictionary = self.items[indexPath.row] as Dictionary<String, AnyObject>

        let cell = tableView.dequeueReusableCellWithIdentifier( (item["type"] as String) + "Cell", forIndexPath: indexPath ) as FeedViewCell

        cell.fillData(item)

        return cell as UITableViewCell
    }

    func feedLoaded(){

        tableView.reloadData()

    }

    func feedLoad(){
        // Feed Load Logic which will call next line once feed loaded

        self.feedLoaded()
    }

}

如果行高为UITableViewAutomaticDimension,则UITableViewWrapperView大小不会随着内容而变化

2 个解决方案

#1


0  

I am assuming you have auto layout configured for your cell, as that's the only way to get this working. Auto Layout should be able to calculate the height based on your cell constraints.

我假设您已经为您的单元格配置了自动布局,因为这是使这个工作的唯一方法。自动布局应该能够根据单元格约束计算高度。

So you should have constraints between your views vertically and between the views on the top and bottom edges the super view (in this case the cell view).

因此,你应该在视图的垂直方向和视图顶部和底部边缘的视图之间有约束(在这里是单元格视图)。

Suppose you have a cell with title and description, like this:

假设您有一个带有标题和描述的单元格,如下所示:

|------------------|
| Title            |
|                  |
| Description with |
| long multiple    |
| lines of text    |
|------------------|

In this example you have to set add a constraint with a vertical space between title and description, a constraint of vertical space between the title's top and the cell view top and a vertical space between the description's bottom and the cell view's bottom.

在本例中,您必须设置一个具有标题和描述之间垂直空间的约束,标题顶部和单元格视图顶部之间垂直空间的约束,以及描述底部和单元格视图底部之间的垂直空间的约束。

#2


0  

The documentation for estimatedHeightForRowAtIndexPath() says that the value retuned by that call is the placeholder value for all till a scroll event occurs. I am guessing that when the scroll even does occur, heightForRowAtIndexPath will get called. So:

estimatedHeightForRowAtIndexPath()的文档说,该调用重新调优的值是所有值的占位符值,直到发生滚动事件。我猜想,当滚动甚至出现时,将调用heightForRowAtIndexPath。所以:

  1. Change the value for estimated height and see if that is the fixed height that you observe.
  2. 更改估计高度的值,看看这是否是您观察到的固定高度。
  3. Does heightForRow get called when you scroll? If not, may be there are other issues about scrolling, such as contentSize etc., that need addressing so that scrolling does take place.
  4. 当你滚动时,heightForRow会被调用吗?如果没有,可能还有其他关于滚动的问题,比如内容大小等,需要寻址,以便滚动。

#1


0  

I am assuming you have auto layout configured for your cell, as that's the only way to get this working. Auto Layout should be able to calculate the height based on your cell constraints.

我假设您已经为您的单元格配置了自动布局,因为这是使这个工作的唯一方法。自动布局应该能够根据单元格约束计算高度。

So you should have constraints between your views vertically and between the views on the top and bottom edges the super view (in this case the cell view).

因此,你应该在视图的垂直方向和视图顶部和底部边缘的视图之间有约束(在这里是单元格视图)。

Suppose you have a cell with title and description, like this:

假设您有一个带有标题和描述的单元格,如下所示:

|------------------|
| Title            |
|                  |
| Description with |
| long multiple    |
| lines of text    |
|------------------|

In this example you have to set add a constraint with a vertical space between title and description, a constraint of vertical space between the title's top and the cell view top and a vertical space between the description's bottom and the cell view's bottom.

在本例中,您必须设置一个具有标题和描述之间垂直空间的约束,标题顶部和单元格视图顶部之间垂直空间的约束,以及描述底部和单元格视图底部之间的垂直空间的约束。

#2


0  

The documentation for estimatedHeightForRowAtIndexPath() says that the value retuned by that call is the placeholder value for all till a scroll event occurs. I am guessing that when the scroll even does occur, heightForRowAtIndexPath will get called. So:

estimatedHeightForRowAtIndexPath()的文档说,该调用重新调优的值是所有值的占位符值,直到发生滚动事件。我猜想,当滚动甚至出现时,将调用heightForRowAtIndexPath。所以:

  1. Change the value for estimated height and see if that is the fixed height that you observe.
  2. 更改估计高度的值,看看这是否是您观察到的固定高度。
  3. Does heightForRow get called when you scroll? If not, may be there are other issues about scrolling, such as contentSize etc., that need addressing so that scrolling does take place.
  4. 当你滚动时,heightForRow会被调用吗?如果没有,可能还有其他关于滚动的问题,比如内容大小等,需要寻址,以便滚动。