简体   繁体   中英

How to have an editable multine UITextView in UITableViewCell

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 UITableViewController instead of the view itself (the UITableViewCell ).

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 textViewDidChange method, figure out what the new height of your cell should be, update your data structure to reflect that, and call reloadRowsAtIndexPaths: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 recommendedRowHeight property to your custom UITableViewCell .

Then, you can either observe this property from your UITableViewController or implement a delegate protocol with a method along the lines of:

- (void)recommendedRowHeightDidChange
// or 
- (void)recommendedRowHeightDidChangeTo:(CGFloat)newHeight

Then, when your height changes, update your recommendedRowHeight property 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: .

You can add your tableview controller object as a weak reference to your tableview cell class. And in tableview controller you can have a method which will be called from tableview cell class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM