Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel doesn't do multiline in a custom UITableViewCell on launch but does after scrolled-back

edit #1

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:

enter image description here

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:

enter image description here

and the layout for the first label:

enter image description here

like image 436
timpone Avatar asked Jul 05 '15 04:07

timpone


3 Answers

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.

like image 87
Jatin Patel - JP Avatar answered Nov 19 '22 12:11

Jatin Patel - JP


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
  }
like image 33
Gaurav Patel Avatar answered Nov 19 '22 10:11

Gaurav Patel


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.enter image description here

like image 2
Saikiran Komirishetty Avatar answered Nov 19 '22 12:11

Saikiran Komirishetty