Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 更改自定义UITableViewCell类中的项_Swift_Uitableview - Fatal编程技术网

Swift 更改自定义UITableViewCell类中的项

Swift 更改自定义UITableViewCell类中的项,swift,uitableview,Swift,Uitableview,我有两个类-一个UITableViewController和一个自定义UITableViewCell。我想更改UITableViewController的单元格高度,因此我实现了以下功能: override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return tableView.frame.height*(1/12) } 而且它有效!单元

我有两个类-一个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的默认运行时表视图创建单元格,然后更改单元格大小,但单元格内的所有内容仍为原始默认值的大小。我需要和牢房一样大。有什么想法吗?
注意-我添加了一个边框,以便您可以查看单元格与项目的比较大小。

框架布局无助于创建动态高度表您必须使用dynamic tableViewCells并创建单元格的contentView(带约束),以获得当前内容的高度更改

//

//

将其放入
viewDidLoad

 tableview.estimatedRowHeight = 100 
 tableview.rowHeight = UITableViewAutomaticDimension

并且不要执行
heightForRowAt
method

,只需稍加修改,这就是我所需要的工作方式。不需要自动标注尺寸,但与边缘相对应是我想要的!谢谢
 tableview.estimatedRowHeight = 100 
 tableview.rowHeight = UITableViewAutomaticDimension