加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 04:57:46 所属栏目:百科 来源:网络整理
导读:只要我不尝试启用自动调整大小,我的单元格就会显示正常.当我在viewController中添加两行自动调整大小时,配置文件图片,用户名和日期下的所有内容都会消失(当您点击图片时,该单元格应该像Instagram一样). 如果我注释掉tableView.rowHeight = UITableViewAutoma
只要我不尝试启用自动调整大小,我的单元格就会显示正常.当我在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]))

}
}

enter image description here


enter image description here

解决方法

试试这段代码:

注意:两种方法都应该有效.在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

输出:

enter image description here

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读