swift – UITableViewAutomaticDimension不适用于调整单元格高度

swift – UITableViewAutomaticDimension不适用于调整单元格高度,第1张

概述只要我不尝试启用自动调整大小,我的单元格就会显示正常.当我在viewController中添加两行自动调整大小时,配置文件图片,用户名和日期下的所有内容都会消失(当您点击图片时,该单元格应该像Instagram一样). 如果我注释掉tableView.rowHeight = UITableViewAutomaticDimension         tableView.estimatedRowHe 只要我不尝试启用自动调整大小,我的单元格就会显示正常.当我在vIEwController中添加两行自动调整大小时,配置文件图片,用户名和日期下的所有内容都会消失(当您点击图片时,该单元格应该像Instagram一样).

如果我注释掉tableVIEw.rowHeight = UItableVIEwautomaticDimension
        tableVIEw.estimatedRowHeight = 450然后单元格显示的确如此,但显然不会调整大小.

这是我目前的代码:
在我的tableVIEwController中:

tableVIEw.rowHeight = UItableVIEwautomaticDimension    tableVIEw.estimatedRowHeight = 450

在我的cellVIEwController中

@IBOutlet weak var profilePic: UIImageVIEw!@IBOutlet weak var usernameBtn: UIbutton!@IBOutlet weak var dateLbl: UILabel!@IBOutlet weak var mainPic: UIImageVIEw!@IBOutlet weak var likeBtn: UIbutton!@IBOutlet weak var commentBtn: UIbutton!@IBOutlet weak var moreBtn: UIbutton!@IBOutlet weak var likeLbl: UILabel!@IBOutlet weak var TitleLbl: UILabel!@IBOutlet weak var uuIDLbl: UILabel!overrIDe func awakeFromNib() {    super.awakeFromNib()    let wIDth = UIScreen.main.bounds.wIDth    //allow constraints    profilePic.translatesautoresizingMaskIntoConstraints = false    usernameBtn.translatesautoresizingMaskIntoConstraints = false    dateLbl.translatesautoresizingMaskIntoConstraints = false    mainPic.translatesautoresizingMaskIntoConstraints = false    likeBtn.translatesautoresizingMaskIntoConstraints = false    commentBtn.translatesautoresizingMaskIntoConstraints = false    moreBtn.translatesautoresizingMaskIntoConstraints = false    likeLbl.translatesautoresizingMaskIntoConstraints = false    TitleLbl.translatesautoresizingMaskIntoConstraints = false    uuIDLbl.translatesautoresizingMaskIntoConstraints = false    let pictureWIDth = wIDth - 20    //constraints    //vertical constraints: objects that are directly above or below each other are in same constraint,if not vertical it must go in another constraint    contentVIEw.addConstraints(NSLayoutConstraint.constraints(        //V: = vertical constraint ; | = top border ; -5-[profilePic(30)] make profilePic with height of 30 and place 5 points below top border ; -10-[mainPic(\(pictureWIDth))] make pic with height of pictureWIDth and place 10 points below profilePic ; -5-[like(30)] make like button height 30 points and put 5 points below mainPic        withVisualFormat: "V:|-10-[profilePic(30)]-10-[mainPic(\(pictureWIDth))]-5-[like(30)]-5-[Title]",options: [],metrics: nil,vIEws: ["profilePic":profilePic,"mainPic":mainPic,"like":likeBtn,"Title":TitleLbl]))    contentVIEw.addConstraints(NSLayoutConstraint.constraints(        //place username 5 points below top border        withVisualFormat: "V:|-10-[username]",vIEws: ["username": usernameBtn]))    contentVIEw.addConstraints(NSLayoutConstraint.constraints(        //place comment button 10 points below mainPic        withVisualFormat: "V:[mainPic]-5-[comment]",vIEws: ["mainPic":mainPic,"comment":commentBtn]))    contentVIEw.addConstraints(NSLayoutConstraint.constraints(        withVisualFormat: "V:|-10-[date]",vIEws: ["date":dateLbl]))    contentVIEw.addConstraints(NSLayoutConstraint.constraints(        withVisualFormat: "V:[mainPic]-10-[likes]","likes":likeLbl]))    contentVIEw.addConstraints(NSLayoutConstraint.constraints(        withVisualFormat: "V:[mainPic]-5-[more]","more":moreBtn]))    contentVIEw.addConstraints(NSLayoutConstraint.constraints(        withVisualFormat: "H:|-10-[profilePic(30)]-10-[username]-10-|","username":usernameBtn]))    contentVIEw.addConstraints(NSLayoutConstraint.constraints(        withVisualFormat: "H:|-10-[mainPic]-10-|",vIEws: ["mainPic":mainPic]))    contentVIEw.addConstraints(NSLayoutConstraint.constraints(        withVisualFormat: "H:|-15-[like(30)]-10-[likes]-30-[comment]",vIEws: ["like":likeBtn,"likes":likeLbl,"comment":commentBtn]))    contentVIEw.addConstraints(NSLayoutConstraint.constraints(        withVisualFormat: "H:[more]-15-|",vIEws: ["more":moreBtn]))    contentVIEw.addConstraints(NSLayoutConstraint.constraints(        withVisualFormat: "H:|-15-[Title]-15-|",vIEws: ["Title":TitleLbl]))    contentVIEw.addConstraints(NSLayoutConstraint.constraints(        withVisualFormat: "H:[date]-10-|",vIEws: ["date":dateLbl]))

}
}


解决方法 试试这段代码:

注意:两种方法都应该有效.在Swift 3中进行了测试.

方法1:

func tableVIEw(_ tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int {   tableVIEw.estimatedRowHeight = 44.0 // standard tableVIEwCell height   tableVIEw.rowHeight = UItableVIEwautomaticDimension return yourArrayname.count }

方法2:

overrIDe func tableVIEw(_ tableVIEw: UItableVIEw,heightForRowAt indexPath: IndexPath) -> CGfloat {  tableVIEw.estimatedRowHeight = 44.0 // standard tableVIEwCell height  tableVIEw.rowHeight = UItableVIEwautomaticDimension return UItableVIEwautomaticDimension } overrIDe func tableVIEw(_ tableVIEw: UItableVIEw,estimatedHeightForRowAt indexPath: IndexPath) -> CGfloat { return UItableVIEwautomaticDimension }

注意:您可能需要将此代码放在cellForRowAt中

yourCellname.sizetoFit()    cell.textLabel?.numberOflines = 0

输出:

总结

以上是内存溢出为你收集整理的swift – UITableViewAutomaticDimension不适用于调整单元格高度全部内容,希望文章能够帮你解决swift – UITableViewAutomaticDimension不适用于调整单元格高度所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1016823.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存