简体   繁体   中英

Can I edit the UITableView cell's textLabel directly?

Ok, I know it seems like there are a bunch of questions on this topic on here already, but I haven't been able to find anything that I need.

I have a UITableViewCell that I want to be able to select (process the touch through didSelectRowAtIndexPath: ) and then respond to that selection by allowing the user to begin editing the contents of the cell's textLabel. I need a label because I need multiple lines of text.
I need the height of the cell to grow as the user enters more and more text.

Do I need to subclass UITableViewCell and have my own label or textView I guess? Or can I use the textLabel that is already there?

Please advise!

What I may suggest doing, or at the least attempting as your best bet, is to create an array of text strings (NSString) in order for each cell. Of course you know you can edit an array at a specific point, so knowing what cell you selected allows you to know which string to edit. From here when a label text has been edited simply call: [tableView reloadData]

And it should update that for you!

I have the solution for you, What exactly you need. Even I have faced this problem & found the solution too. Check with this link . You need to work more on this example to dynamic sizing the cell

  • First of all, you should be able to calculate frame of Text using NSString's sizeWithFont:constrainedToSize:lineBreakMode: method.

  • In you UITableView delegate implement tableView:heightForRowAtIndexPath: method. Calculate height for text in each cell, also include paddings and everything else you might need. I would also cache this value until text is changed.

  • Subclass UITableViewCell. You have two variants here:

    1. Add UITextView to Cell every time user wants to edit text and remove it back after that.
    2. Use UITextView to display and edit text (it has editable property). But I'm not sure how speedy it will be inside table, I haven't tested this personally.

    I use the first variant in my app.

  • You can subclass UITextView or use its delegate methods to update its height when user types something.

  • Propagate the height change to your UITableView delegate.

Call the following code to update cells animated:

[self.tableView beginUpdates];
[self.tableView endUpdates]; 

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