【问题标题】:Changing items within custom UITableViewCell Class更改自定义 UITableViewCell 类中的项目
【发布时间】:2018-06-10 17:20:39
【问题描述】:

我有 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
    }

}

所以,我现在编辑控制器内部的函数来调用单元格。

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 创建单元格,然后更改单元格大小,但单元格内的所有内容仍然是原始默认值的大小。我需要它是单元格的大小。有任何想法吗? 注意 - 我添加了一个边框,以便您可以看到单元格与项目相比的大小。

【问题讨论】:

    标签: swift uitableview


    【解决方案1】:

    框架布局无法帮助创建动态高度表,您必须使用动态 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方法

    【讨论】:

    • 稍作修改,这就是我需要的方式。不需要自动尺寸,但限制到边缘是我正在寻找的!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    相关资源
    最近更新 更多