0

I have UITableView with dynamic cells. In viewDidLoad I have NSURLSession which download some data to common variable someDataArray: [NSString]. I'm using AutoLayout and UILabel in my cell has constraints like this. When view is loaded I want see all cells are same size (for example: 100). But when cell is tapped I want to change height of this cell to height size of UIlabel frame So my code looks like:

class someVC: UIViewController, UITableViewDelegate
{
  var someDataArray: [NSString] = []
  var cellTapped = 0

  override func viewDidLoad() {
    //NSURLSession download to someDataArray[]
  }

  func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! 
  {
     let cell = self.table.cellForRowAtIndexPath(indexPath) as customCellVC
     cell.someTextView.text = someDataArray[indexPath.row]
  }

  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = self.table.cellForRowAtIndexPath(indexPath) as customCellVC

      if(cellTapped == 0) {
        cellTapped = 1
        table.beginUpdates()
        table.endUpdates()
      } else {
        cellTapped = 0
        table.beginUpdates()
        table.endUpdates()
      }
    }

 func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat 
 {

   // heightForRowAtIndexPath call first, how to calculate the size of frame uilabel? When it's tapped or not
   // how to make all UILabel one size by load but sizetofit by tap on it?
 }
}
Community
  • 1
  • 1
Max
  • 1,341
  • 3
  • 20
  • 39

1 Answers1

1

you could use Autolayout to calculate the height of the row. Check out this:

https://github.com/smileyborg/TableViewCellWithAutoLayoutiOS8

Or: (so i solved this, because it fits better to my requirements) is:

Calculate the height for the Label:

func getLabelHeight(mytext: String, fontSize: CGFloat, width: CGFloat)->CGFloat {

    let font = UIFont.systemFontOfSize(fontSize)
    let size = CGSizeMake(width,CGFloat.max)
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineBreakMode = .ByWordWrapping;
    let attributes = [NSFontAttributeName:font,
        NSParagraphStyleAttributeName:paragraphStyle.copy()]

    var text = mytext as NSString
    let rect = text.boundingRectWithSize(size, options:.UsesLineFragmentOrigin, attributes: attributes, context:nil)
    return rect.size.height
}

This will return you the calculated height of a label for a specific width. Sou you can calculate this on ViewDidLoad (for the first Time and set it into an Dictionary with the Indexpath.row as Index) and then use this values for "heightForRowAtIndexPath". So you are able to reload your specific cell too (with your new height)

Then you can still use "SizeToFit" width a specific label width.

derdida
  • 14,784
  • 16
  • 90
  • 139
  • Thanks for this func. I'll try it and let you know – Max Sep 25 '14 at 11:52
  • Be careful when you are only using "sizeToFit" - because it will on default not using the given width in your frame. You should also set your frame of your label to the calculated height. – derdida Sep 25 '14 at 11:57