I have a custom tableview cell current, and I have a UILabel that is being filled with text that is multiple lines long. However, I don't want to change the size of the custom cell, so I want the UILabel to a adapt to the given size. Here is my problem though:
On first load of the table, the UILabel will not reflect the proper height, and won't show the word wrap for the first 3 or 4 cells
Once I start scrolling, the new cells reflect the proper height, and scrolling back causes the old cells to reflect the proper height.
If I keep scrolling back and forth, the labels shrink their width and become squished.
I am using auto layout too. Here is the code for the label as it is formatted:
private func assignBusAndRouteTextForIndex(card: ETACard, index: Int) {
card.busNumberLabel.text = jsonValueForIndexAndSubscript(index, string: "rd")
var route = jsonValueForIndexAndSubscript(index, string: "fd")
route = route.stringByReplacingOccurrencesOfString("&", withString: "&")
print(route)
card.routeLabel.text = route
card.routeLabel.adjustsFontSizeToFitWidth = true
card.routeLabel.sizeToFit()
}
private func jsonValueForIndexAndSubscript(index: Int, string: String) -> String {
return self.items.arrayValue[index][string].description;
}
And here are my auto layout constraints:
And finally, my custom tableview cell class looks like this:
import Foundation
import UIKit
//Dependancies
import AnimationsFramework
class ETACard: UITableViewCell {
@IBOutlet weak var card: UIView!
@IBOutlet weak var busNumberLabel: UILabel! // Contains 'Bus:'
@IBOutlet weak var routeLabel: UILabel! // Contains 'Via:'
@IBOutlet weak var circleView: UIView! // Contains the timing circle
@IBOutlet weak var timeLabel: UILabel! // Contains the arrival time
override func layoutSubviews() {
cardSetup()
self.addSubview(card)
}
func cardSetup() {
self.card.alpha = 1
self.card.layer.masksToBounds = false
self.card.layer.cornerRadius = 1
self.card.layer.shadowOffset = CGSizeMake(-0.2, 0.2)
self.card.layer.shadowRadius = 1
let path = UIBezierPath(rect: self.card.bounds)
self.card.layer.shadowPath = path.CGPath
self.card.layer.shadowOpacity = 0.2
}
func renderCircleForBusTime(busTime: Int) {
ShapeRenderer.renderCircleForBusTime(circleView, busTime: busTime)
}
func removeCircleFromCard(view: UIView) {
ShapeRenderer.removeRenderedCircle(view);
}
}
EDIT:
Here are some progressive screenshots of what I am talking about. Between each screenshot is a scroll to the bottom of the list, then back up.
Please note how the text progressively shrinks and starts to get cut.