首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用UITableViewAutomaticDimension的约束错误

使用UITableViewAutomaticDimension的约束错误
EN

Stack Overflow用户
提问于 2018-05-19 18:03:29
回答 1查看 362关注 0票数 1

我有一个困难的动态高度定制tableView单元。

因此,我得到了“不能同时满足约束”。

我认为UITableViewAutomaticDimension给出了这个问题。在这个例子中,它期望88.3333的高度,但它可能是不正确的。(但大多数时候,我都不明白发生了什么事。)

我做错什么了,有人能帮忙吗?什么都试过了。

代码:

代码语言:javascript
复制
var cellHeights: [IndexPath : CGFloat] = [:]
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cellHeights[indexPath] = cell.frame.size.height

        if indexPath.row == UserWorldMessagesStore.shared.worldMessages.count - 1 && userWorldMessagesCanLoadMore == true {
            loadOlderOwnWorldMessages()
        }
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        if cellHeights[indexPath] != nil {
            return CGFloat(Float(cellHeights[indexPath] ?? 0.0))
        }
        else {
            return UITableViewAutomaticDimension
        }
    }

警告:

代码语言:javascript
复制
2018-05-19 19:58:54.879876+0200 PipeTest[3378:1282243] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x1c0297fc0 V:|-(10)-[UILabel:0x125d5db40'Heyy']   (active, names: '|':UIView:0x125d03400 )>",
    "<NSLayoutConstraint:0x1c0298010 V:[UILabel:0x125d5db40'Heyy']-(10)-|   (active, names: '|':UIView:0x125d03400 )>",
    "<NSLayoutConstraint:0x1c0298100 PipeTest.profilePictureImageView:0x125d56fc0.height == 22   (active)>",
    "<NSLayoutConstraint:0x1c0298240 UIView:0x125e58150.height == 27   (active)>",
    "<NSLayoutConstraint:0x1c0298600 PipeTest.profilePictureImageView:0x125d56fc0.top == UITableViewCellContentView:0x125d50850.topMargin   (active)>",
    "<NSLayoutConstraint:0x1c02986a0 V:[PipeTest.profilePictureImageView:0x125d56fc0]-(3)-[UIView:0x125d03400]   (active)>",
    "<NSLayoutConstraint:0x1c02988d0 UIView:0x125e58150.bottom == UITableViewCellContentView:0x125d50850.bottomMargin   (active)>",
    "<NSLayoutConstraint:0x1c0298970 V:[UIView:0x125d03400]-(7)-[UIView:0x125e58150]   (active)>",
    "<NSLayoutConstraint:0x1c0299230 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x125d50850.height == 88.3333   (active)>"
)
EN

回答 1

Stack Overflow用户

发布于 2018-05-19 19:11:17

完整的警告文本应该包括以下内容

代码语言:javascript
复制
Will attempt to recover by breaking constraint  
<NSLayoutConstraint:somePointer V:some constraint>

将该约束的优先级降低到999应该可以消除此警告。

这样做的原因是:

代码语言:javascript
复制
<NSLayoutConstraint:0x1c0299230 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x125d50850.height == 88.3333   (active)>

这是一个系统提供的约束,它告诉表视图单元格的高度应该是什么,它的值基于您从estimatedHeightForRowAt返回的内容。在计算适当的单元格大小之前抛出警告(最终将正确计算),因此只需要降低其他约束的优先级是安全的,因此系统--前提是优先。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50428115

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档