Now with editing and rotation

I went on and extended the autoresizing table cells to handle interface rotation and table editing as well. In both these cases, the width of the table cell content view changes, so the height of the text view needs to be recalculated. To achieve that, you need to wait to measure the new width of the text view until everything has settled down, but this turns out to be surprisingly difficult. The only functions I found that were called late enough in the view update cycle to give me the new definite text view width were didRotateFromInterfaceOrientation in the controller for rotations, and didTransitionToState in the table view cell for editing state changes.

In the controller:

override func didRotateFromInterfaceOrientation(

                fromInterfaceOrientation: UIInterfaceOrientation) {

  updateVisibleCells()

  // after rotation, the cursor has a tendency to keep indicating editing

  // even though the current view doesn’t respond to typing

  // anymore. Ending editing cleans that up.

  view.endEditing(true)

}

 
As you can see, I also had to add the endEditing() call to get rid of ambiguous editing state.

In the table view cell child class:

override func didTransitionToState(state: UITableViewCellStateMask) {

  super.didTransitionToState(state)

  updateTextViewSize()

}

 
These two methods work fine, but there is one very irritating problem left, namely that the animations don’t coincide. When rotating or changing editing state, the built-in table animations happen first, and only then does the text view animate to its new size. I imagine you could wrap the animations in CATransition calls, but if you do that, you need to know beforehand how wide the text view will be, and to do that, you need to know the width of the table in both orientations, and the change in size of the content view of the table cell. I can find no properties in the Cocoa classes that can tell me any of this. As the project is now, I do find out after the fact what the new size is, but that’s too late. If anyone knows a better method, I’m all ears.
 
The entire project can be downloaded here. As before, it’s in Swift 2.x and Xcode 7.3.
 

Leave a Reply

Your email address will not be published. Required fields are marked *