Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS8 change height of dynamic cell "now", re internal cell content

Here's a dynamic cell

enter image description here

Note - in the example, the text is not data driven. It's just some text local to the cell (consider, say, a help text). At runtime, change the .text of the UILabel from one word to many lines, using a button actually inside the cell. iOS perectly resizes the cell and table....

... but only when the cell is scrolled offscreen, and then on again.

How to alert the table view to recalculate everything "now" ?


(Please note, this question ONLY in the case of iOS8+, Xcode7+, autolayout for dynamic cell heights.)

like image 730
Fattie Avatar asked Sep 19 '15 21:09

Fattie


2 Answers

I'm presuming you're not setting the text property of the UILabel inside cellForRowAtIndexPath but rather somewhere else (or doing it asynchronously). If that's the case, I wouldn't update the UI there. Rather, I'd update the model backing the table and then call reloadRowsAtIndexPaths. That will let cellForRowAtIndexPath call again, but unlike reloading the whole table, this will gracefully keep the contentOffset of the tableview right where it is.

I know this all sounds unnecessarily complicated, but the bottom line is that you don't own this view, the table view does. It has to do all sorts of stuff above and beyond updating the cell. I.e., if the cell grew, figure out which cells scrolled out of view and dequeue them. If the cell shrunk, figure out which cells scrolled into view as a result.

It's a surprisingly complex dance. You can try calling setNeedsLayout on the cell, but I wouldn't expect that to work (and even if it does, it is a fragile approach). The table view is responsible for managing its cells, so if you really should just update model and reload that one cell.

like image 36
Rob Avatar answered Nov 15 '22 06:11

Rob


Changing height

So basically, there are two ways to do:

The first one is to actually reload the cell (not the tableview). Reloading will call new heightForRow (don't forget to purge cache, if you are caching the sizes), which will return proper new height:

 let indexPaths = [NSIndexPath(forRow: ~the rows in question~, inSection: 0)]
 self.table.reloadRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)

(Note however that this often involves reloading more than one row; notably if you select/deselect, you have to reload all rows changed.)

If however you ONLY want to change the size of the cell and the content per se, and did not really change the data content ... so for example:

  • you clicked some button and you assigned new local text in the cell to outlets (perhaps a help text):

  • you changed only the LAYOUT of the cell. for example, you made a font larger, or changed the margin of a block of text so that the height of a block of text changed, so indeed the height of the overall cell changed:

In that case instead of reloading, just call the following, which forces the tableview to basically do all animations, and for that it needs new heights, so it requests it:

 self.table.beginUpdates()
 self.table.endUpdates()

The true solution

I see what your problem is. You are trying to change the height of the cell from the actual cell - but you will not succeed in that -> and you should not. See, the cell is view, and view should not have any idea about its data whatsoever - view is what presents. If you need any changes, you should inform your controller to do so. To do that, you can use notifications, but preferably protocols / delegates.

So at first you create protocol in your cell, which will be used to inform the controller, that there is a change:

 protocol MyCellDelegate {

    func buttonTappedForCell(cell : UITableViewCell)
 }

Now, you need to conform to that protocol in your view controller that contains table:

 class MyClassWithTableView : MyCellDelegate

Lastly, you need to declare delegate in the cell:

 class MyCell {
     var delegate : MyCellDelegate
 }

And assign it in the configuration of the cell, which you probably have in the view controller:

 cell.delegate = self

This is the basic setup for all the delegates / protocols really, and now, when you click on your button, you can forward the action to your controller:

 @IBAction myButtonTouchUpInside() {

     self.delegate.buttonTappedForCell(self)
 }

After doing all that, proceed as in part 1. That is to say, either reloadRowsAtIndexPaths or a beginUpdates / endUpdates pair as explained above.

Hope it helps!

like image 112
Jiri Trecak Avatar answered Nov 15 '22 04:11

Jiri Trecak