ios – 如果行高度为UITableViewAutomaticDimension,则UITableViewWrapperView的大小不会随内容而改变

如果行高为UITableViewAutomaticDimension,则UITableViewWrapperView的大小不会随内容而改变.

我正在尝试使用TableView和动态高度行创建Feed列表.
TableView是可滚动的,但UITableViewWrapperView看起来不会改变它的大小!

下面是视图层次结构的代码和截图.

我觉得很奇怪

码:

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()
    }

}

解决方法

我假设你为你的单元格配置了自动布局,因为这是唯一的方式来使其工作.自动布局应该能够根据您的单元格限制来计算高度.

因此,您应该在垂直视图之间以及超视图(在这种情况下为单元格视图)的顶部和底部边缘的视图之间存在约束.

假设你有一个标题和描述的单元格,像这样:

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

在这个例子中,你必须设置一个在标题和描述之间的垂直空间的约束,标题的顶部和单元格视图顶部之间的垂直空间的约束以及描述底部和单元格视图底部之间的垂直空间.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...