Dane
Dane

Reputation: 45

Swift UITableView will not resize to a larger size

I have a segmented control that allows a user to select the size on an image in the UITableView. when the user selects a size that is smaller than the last, the tableview resizes and reloads perfectly but once a user clicks on a size larger than the current size, nothing happens. I have print statements in my code so I know that each cell is being remade to the right constraints and size but the tableview still shows the same as it was before selecting a larger size.

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
 }

Upvotes: 0

Views: 49

Answers (1)

Dane
Dane

Reputation: 45

because it uses a reusable cell, I had to clear all the constraints before changing them. for example: imageview.removeConstraints(imageview.constraints())

Upvotes: 1

Related Questions