-1

I have a UITableViewCell. Inside of the cell I have UILabel and UIButton, when user clicks the button the text expands but cell doesn't. So I want the cell to increase it's height with the label.

I have tried this code but it giving me a crash (unexpectedly found nil)

tableView.reloadRows(at: [IndexPath(row: 0, section: 0)], with: .fade)

tried this one also, again crash

tableView.rowHeight = cellHeightNumber

So basically I want it to work like this

Before expanding:

Before expanding

After expanding:

After expanding

Vinodh
  • 5,262
  • 4
  • 38
  • 68
  • Turn on exception breakpoints in Xcode (see here: http://stackoverflow.com/questions/17802662/exception-breakpoint-in-xcode), and then you can figure out where your crash happens. That may help to solve your problem. – koen Feb 06 '17 at 16:25
  • Can you post the actual crash log? This will help diagnose as to what's going wrong and will give some context as to the direction you should take. – Pratik Patel Feb 06 '17 at 16:27
  • Possible duplicate of [Using Auto Layout in UITableView for dynamic cell layouts & variable row heights](http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights) – dahiya_boy Feb 06 '17 at 16:41
  • @the_dahiya_boy Nope – Asilbek Yokubov Feb 06 '17 at 17:07
  • It crashes in **reloadRows** and says **fatal error: unexpectedly found nil while unwrapping an Optional value** @PratikPatel – Asilbek Yokubov Feb 06 '17 at 17:09
  • Please share the code where that error occurs in your question. – koen Feb 06 '17 at 17:29
  • First of all share your code work and storyboard constraints. Second, use UITableViewAutomaticDimension for row height. Third, use delegate method not property. – dahiya_boy Feb 06 '17 at 17:38
  • 1
    Reason of crash : you forcly unwrapping the Nil object. Check the line of crash, your object probably nil. – dahiya_boy Feb 06 '17 at 17:40

2 Answers2

0

I can give you two solutions:

  1. Ditch the reloadRows and use the reloadData instead, like so: tableView.reloadData()

  2. Wrap your reloadRows between the beginUpdates and endUpdates, like so:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        tableView.beginUpdates()
        // Your reloadRows codes here
        tableView.endUpdates()
    }
    
Glenn Posadas
  • 12,555
  • 6
  • 54
  • 95
0

You should use

UITableViewAutomaticDimension

provides a solution for displaying dynamic content. Use below code in viewDidLoad:

tableView.estimatedRowHeight = YourTableViewHeight tableView.rowHeight = UITableViewAutomaticDimension

Use this link : http://www.appcoda.com/self-sizing-cells/

Also set number of line 0 for label in table cell.

Hope it helps. :)

Hemant Solanki
  • 894
  • 10
  • 24