I’ve got a UITextView inside a UITableViewCell subclass. I have no problem getting the new height of the Text view and cell. The problem I have is telling the UITableView to update.
I have implemented heightForRowAtIndexPath: to return the live height of the cell as the TextView expands.
But somewhere `[tableView beginUpdates]; [tableView endUpdates]; must be called.
How? Should I add a delegate property to the UITableViewCell which is set to the UITableViewController subclass? And then send a delegate message when the cell expands height and the Tableview needs to update? It seems a little weird to have a delegate between the UITableViewCell and Controller?
I tried using NSNotificationCenter, but I have more than one editable cell, and more than tableview of this nature. So there is no way to register only for notifications for the cells without copying and pasting the same line over again, which isn’t nice (as the cells are created in IB, and are not in an array or set), and having multiple tableviews means an exception occurs on the other table view as it is told to update but nothing changes.
I’ve seen lots of questions and answers on this topic, but when it comes to updating the tableview they all just say “now update the tableview” and not how to. So how do I telly he tableview to update, from one of it’s cells?
I would think that this behavior would be best implemented in the
UITableViewControllerinstead of the view itself (theUITableViewCell).Your controller is responsible for setting cell height, and typically will be the delegate for your
UITextView‘s, so let it handle all of this.In your
textViewDidChangemethod, figure out what the new height of your cell should be, update your data structure to reflect that, and callreloadRowsAtIndexPaths:withRowAnimation:to have it actually change.Edit:
So since you didn’t like my first suggestion, another way to do this would be to add a
recommendedRowHeightproperty to your customUITableViewCell.Then, you can either observe this property from your
UITableViewControlleror implement a delegate protocol with a method along the lines of:Then, when your height changes, update your
recommendedRowHeightproperty and call your delegate’s method if you go that route.Either way, once your controller figures out that the recommended row height of a cell has changed, it can do what it is supposed to do. Update your data structures reflecting the current row heights and then call
reloadRowsAtIndexPaths:withRowAnimation:.