I added a link to the project on github here: https://github.com/trestles/testtable
This is really my first time dealing with Autolayout so I expect I am doing some amateur mistakes. Honestly, I know how I'd do this manipulating frames but can't get to work properly with autolayout with the clipping of content. Part of the questions is should I just be using frames if we are always in portrait mode?
I have a custom UITableViewCell where I have a few UILabels. They are set to numberOfLines=0. Sometimes, they will truncate the text. Like this:
How do I fix this? I have tried to reloadData in viewDidLoad but that didn't seem to matter. Most times, when you scroll, it fixes itself (but not always). It can be any three of the UILabels and is indepedent of the amount of text. My first time using UILabels with Auto Layout so most likely some mistake I have made. Here's what my UILabel properties are:
and the layout for the first label:
Your auto layout is perfect, just issue occurred because you are setting auto layout
with default text in your xib. and in you viewDidLoad
, where you are updating UILabel
text but not updating layout
programatically. so just one line is left as below.
self.mainTV.layoutIfNeeded();
Add above line before reload UITableView
in viewDidLoad
method. all the things working fine.
Example :
override func viewDidLoad() {
super.viewDidLoad()
self.mainTV.dataSource=self
self.mainTV.delegate=self
self.mainTV.rowHeight = UITableViewAutomaticDimension
self.mainTV.estimatedRowHeight = 84.0
//self.mainTV.registerClass(MyTableViewCell.self, forCellReuseIdentifier: "MyCustomCell")
var tmps = [String]()
tmps.append("ABC But here is an artist. He desires to paint you the dreamiest, shadiest, quietest, most enchanting bit of romantic landscape in all the valley of the Saco. What is the chief element he employs?")
tmps.append("DEF But here is an artist.")
tmps.append("GHI But here is an artist. He desires to paint you the dreamiest, shadiest, quietest, most enchanting bit")
for var i=0; i<10; i++
{
var menuItem=EKMenuItem()
let randomIndex1 = Int(arc4random_uniform(UInt32(tmps.count)))
menuItem.header = "\(i) \(tmps[randomIndex1])"
let randomIndex2 = Int(arc4random_uniform(UInt32(tmps.count)))
menuItem.detail = "\(i) \(tmps[randomIndex2])"
let randomIndex3 = Int(arc4random_uniform(UInt32(tmps.count)))
menuItem.price = "\(i) \(tmps[randomIndex3])"
//tmpItem.price = "my price"
dataItems.append(menuItem)
}
self.view.backgroundColor = UIColor.greenColor()
self.mainTV.layoutIfNeeded();
self.mainTV.reloadData()
// Do any additional setup after loading the view, typically from a nib.
}
Hope this help you.
update your cell constraint and layout
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCustomCell") as! MyTableViewCell
cell.headerLabel.text=dataItems[indexPath.row].header
cell.setHeader(dataItems[indexPath.row].header)
cell.setDetail(dataItems[indexPath.row].detail)
cell.setPrice(dataItems[indexPath.row].price)
// update constraint and layout
cell.layoutIfNeeded()
return cell
}
I have downloaded the source from git. I'm not sure whether this is bug or not. But reloading the table in viewdidappear solved the issue.
override func viewDidAppear(animated: Bool) {
mainTV.reloadData()
}
This is how it will look on simulator when you perform above operation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With