swift - 更改自定义 UITableViewCell 类中的项目

标签 swift uitableview

我有 2 个类 - 一个 UITableViewController 和一个自定义 UITableViewCell。我想更改我的 UITableViewController 的单元格高度,所以我实现了以下内容:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return tableView.frame.height*(1/12)
}

而且有效!单元格高度发生变化。现在,我进入我的自定义 UITableViewCell 类

import UIKit

class TableViewCell: UITableViewCell {

    var time:UILabel = UILabel()
    var name:UILabel = UILabel()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier) // the common code is executed in this super call

        let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.frame.width*0.22, height: self.frame.height))
        imageView.image = UIImage(named: "Person")
        imageView.contentMode = .scaleAspectFit
        imageView.isUserInteractionEnabled = true
        self.addSubview(imageView)

        let tap = UITapGestureRecognizer(target: self, action: #selector(signIn))
        imageView.addGestureRecognizer(tap)

        name = UILabel(frame: CGRect(x: self.frame.width*0.22, y: 0, width: self.frame.width*0.78, height: self.frame.height*0.7))
        name.textColor = UIColor.gray
        name.font = UIFont(name: name.font!.fontName, size: 30)
        name.adjustsFontSizeToFitWidth = true
        self.addSubview(name)

        time = UILabel(frame: CGRect(x: self.frame.width*0.22, y: self.frame.height*0.65, width: self.frame.width*0.78, height: self.frame.height*0.3))
        time.textColor = UIColor.gray
        time.font = UIFont(name: time.font!.fontName, size: 15)
        time.adjustsFontSizeToFitWidth = true
        self.addSubview(time)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    @objc func signIn(tap: UITapGestureRecognizer) {
        let tappedImage = tap.view as! UIImageView
        tappedImage.image = UIImage(named: "PersonClocked")
    }

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

        // Configure the view for the selected state
    }

}

因此,我现在在我的 Controller 中编辑函数以调用单元格。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell:TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell

    cell.layer.borderColor = UIColor.red.cgColor
    cell.layer.borderWidth = 1

    cell.name.text = names[indexPath.row]
    cell.time.text = times[indexPath.row]

    return cell
}

我希望它现在一切正常!但是,事实并非如此。无论出于何种原因,这些方法都会按此顺序调用...

tableview(cellForRowAt) -> TableViewCell(override init) -> tableView(heightForRowAt)'

所以,当我运行它时,它看起来像下面这样。使用 Swift/Apple 的默认运行时 tableView 创建单元格,然后更改单元格大小,但单元格内的所有内容仍然是原始默认值的大小。我需要它是单元格的大小。有任何想法吗? 注意 - 我添加了一个边框,这样您就可以看到单元格的大小与项目的比较。

最佳答案

框架布局无法帮助创建动态高度表,您必须使用动态 tableViewCells 并创建带有约束的单元格的 contentView 以获取当前内容的高度变化

//

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier) // the common code is executed in this super call

    let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.frame.width*0.22, height: self.frame.height))
    imageView.image = UIImage(named: "Person")
    imageView.contentMode = .scaleAspectFit
    imageView.isUserInteractionEnabled = true
    self.addSubview(imageView)

    let tap = UITapGestureRecognizer(target: self, action: #selector(signIn))
    imageView.addGestureRecognizer(tap)

    name = UILabel(frame: CGRect(x: self.frame.width*0.22, y: 0, width: self.frame.width*0.78, height: self.frame.height*0.7))
    name.textColor = UIColor.gray
    name.font = UIFont(name: name.font!.fontName, size: 30)
    name.adjustsFontSizeToFitWidth = true
    self.addSubview(name)

    time = UILabel(frame: CGRect(x: self.frame.width*0.22, y: self.frame.height*0.65, width: self.frame.width*0.78, height: self.frame.height*0.3))
    time.textColor = UIColor.gray
    time.font = UIFont(name: time.font!.fontName, size: 15)
    time.adjustsFontSizeToFitWidth = true
    self.addSubview(time)

    imageView.translatesAutoresizingMaskIntoConstraints = false
    name.translatesAutoresizingMaskIntoConstraints = false
    time.translatesAutoresizingMaskIntoConstraints = false

    imageView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor,constant: 0).isActive = true
    imageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor,constant: 20).isActive = true
    imageView.heightAnchor.constraint(equalToConstant: 30).isActive = true
    imageView.widthAnchor.constraint(equalToConstant: 30).isActive = true

    name.topAnchor.constraint(equalTo: contentView.topAnchor,constant: 20).isActive = true
    name.leadingAnchor.constraint(equalTo: imageView.leadingAnchor,constant: 20).isActive = true
    name.trailingAnchor.constraint(equalTo: contentView.trailingAnchor,constant: -20).isActive = true

    time.topAnchor.constraint(equalTo: name.topAnchor,constant: 20).isActive = true
    time.leadingAnchor.constraint(equalTo: imageView.leadingAnchor,constant: 20).isActive = true
    time.trailingAnchor.constraint(equalTo: contentView.trailingAnchor,constant: -20).isActive = true
    time.bottomAnchor.constraint(equalTo: contentView.bottomAnchor,constant: -20).isActive = true

}

//

把这个放在viewDidLoad

 tableview.estimatedRowHeight = 100 
 tableview.rowHeight = UITableViewAutomaticDimension

并且不实现heightForRowAt方法

关于swift - 更改自定义 UITableViewCell 类中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50786220/

相关文章:

ios - 如何获取当前/去年的天数

swift - 哪些版本的 Xcode 支持哪些版本的 Swift?

swift - xcode 6 中的选项卡栏 Controller 灰色框

ios - 在 UITableViewCell 返回 View 后,NSManagedObject 数据为 <fault> 且实体为空

ios - 如何在 Alamofire 中使用 PUT 请求

ios - 如何向此 UITableView 添加额外的列并填充已获取的数据?

ios - 无法使用自定义单元格加载表格 View

ios - 将解析后的数据保存到数组中

ios - cellForRowAtIndexPath 是如何工作的?

ios - WWDC2019源码在哪里下载?