Swift UITableView将不会调整到更大的大小

Swift UITableView将不会调整到更大的大小,swift,xcode,uitableview,Swift,Xcode,Uitableview,我有一个分段控件,允许用户在UITableView中选择图像的大小。当用户选择的尺寸小于上一个尺寸时,tableview会完全调整大小并重新加载,但一旦用户单击大于当前尺寸的尺寸,则不会发生任何事情。我的代码中有print语句,因此我知道每个单元格都将被重新生成为正确的约束条件和大小,但tableview仍显示与选择更大大小之前相同的内容 var imageSize:FLoat = 160 func changeImageSize(){ // this is called when the s

我有一个分段控件,允许用户在UITableView中选择图像的大小。当用户选择的尺寸小于上一个尺寸时,tableview会完全调整大小并重新加载,但一旦用户单击大于当前尺寸的尺寸,则不会发生任何事情。我的代码中有print语句,因此我知道每个单元格都将被重新生成为正确的约束条件和大小,但tableview仍显示与选择更大大小之前相同的内容

var imageSize:FLoat = 160

func changeImageSize(){ // this is called when the segmented control is selected
    if imageSizeTab.selectedSegmentIndex == 0 {
        self.imageSize = 140
    } else if imageSizeTab.selectedSegmentIndex == 1 {
        self.imageSize = 200
    } else if imageSizeTab.selectedSegmentIndex == 2 {
        self.imageSize = 240
    }
    print("changing image size to ", self.imageSize)
    self.viewableListingsTableView.reloadData()
}

// this is a summary of the creation of the cell. simplified for the sake of this question
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = self.viewableListingsTableView.dequeueReusableCell(withIdentifier: "custom", for: indexPath) as! listingCellTableViewCell
    cell.height = self.imageSize
    cell.layoutSubviews()
}

// this is the UITableView class
class listingCellTableViewCell: UITableViewCell {
    var height:Float? {
        didSet {
            self.setupView()
        }
    }
}


// I deleted the creation of irrelevant views to be easier to read
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    self.addSubview(titleView)
    self.addSubview(firstImageView)
    self.addSubview(distanceView)
    self.addSubview(priceView)
}

func setupView() {
    print("changed image size to ", self.height ?? 0)
    let h = self.height ?? 160
    print("changed h to ", h)
    self.heightAnchor.constraint(equalToConstant: CGFloat(h)).isActive = true

    //self.contentMode = .scaleAspectFit
    firstImageView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
    //firstImageView.rightAnchor.constraint(equalTo: self.heightAnchor)
    firstImageView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    firstImageView.heightAnchor.constraint(equalToConstant: CGFloat(h)).isActive = true
    firstImageView.widthAnchor.constraint(equalToConstant: CGFloat(h)).isActive = true

    titleView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: CGFloat(h + 6)).isActive = true
    titleView.preferredMaxLayoutWidth = self.frame.width - CGFloat(h) - 12
    titleView.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.6).isActive = true
    titleView.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true

    priceView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: CGFloat(h + 6)).isActive = true
    priceView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 6).isActive = true
    priceView.bottomAnchor.constraint(equalTo: distanceView.topAnchor, constant: 0).isActive = true
    priceView.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.2).isActive = true

    distanceView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: CGFloat(h + 6)).isActive = true
    distanceView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 6).isActive = true
    distanceView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -6).isActive = true
    distanceView.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.2).isActive = true
 }

因为它使用了一个可重用的单元,所以我必须在更改它们之前清除所有约束。例如:imageview.removeConstraintsimageview.constraints

使用LayoutFneed而不是使用此layoutsubview,并使用update constraints的名称覆盖表视图单元格中的函数。只需更新图像的约束。请记住,如果您的单元格大小为100,并且您希望更改图像大小240,则该操作无效。您还需要更新单元格大小。