首页 > 解决方案 > Swift UITableView 不会调整到更大的尺寸

问题描述

我有一个分段控件,允许用户在 UITableView 中选择图像的大小。当用户选择的尺寸小于上一个尺寸时,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
 }

标签: swiftxcodeuitableview

解决方案


因为它使用可重复使用的单元格,所以我必须在更改它们之前清除所有约束。例如:imageview.removeConstraints(imageview.constraints())


推荐阅读