【问题标题】:Dynamically resize custom xib cell动态调整自定义 xib 单元格的大小
【发布时间】:2019-10-12 04:38:09
【问题描述】:

所以我的自定义 xib 单元格没有根据单元格内部的 UILabel 动态调整大小时遇到​​了一些问题。我已经设置了

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
    }
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 250
    }

XIB 单元视图控制器

import UIKit

class ReminderTableViewCell: UITableViewCell {

@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var progressBar: UIProgressView!
@IBOutlet weak var notesLabel: UILabel!
@IBOutlet weak var titleLabel: UILabel!
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    backgroundColor = .clear // very important
    layer.masksToBounds = false
    layer.shadowOpacity = 1
    layer.shadowRadius = 4

    layer.shadowOffset = CGSize(width: 2 , height: 2)
    layer.shadowColor = UIColor.darkGray.cgColor

    // add corner radius on `contentView`
    contentView.backgroundColor = .darkGray
    contentView.layer.cornerRadius = 8
    progressBar.transform = progressBar.transform.scaledBy(x: 1, y: 5)
    progressBar.layer.cornerRadius = 15
    progressBar.clipsToBounds = true
    self.layer.sublayers![1].cornerRadius = 15
    self.subviews[1].clipsToBounds = true
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)


    // Configure the view for the selected state
}


func setInfo(title:String, notes:String,date:String){
    titleLabel.text = title
    notesLabel.text = notes
    dateLabel.text = date
}


override func layoutSubviews() {
    super.layoutSubviews()

    contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 10, left: 15, bottom: 15, right: 10))
}

 }

XIB 的限制

外观

我将 titleLabel 和 userNotes 的行都设置为 0,并且两者都有自动换行。

如果你们能帮助我,我会非常感谢。

【问题讨论】:

    标签: swift xcode uitableview xib


    【解决方案1】:

    一旦你应该像这样尝试。

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 返回 250 }

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-15
      • 2016-01-08
      • 2017-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多