Allow user to edit text in UITableView cell

I create a “favorite pages” menu in my tableview application where users can save their favorite web pages and then easily navigate to them through the menu.

For the convenience of remembering which link is, I want to allow the user to click the button that says "edit", somehow select the cell that they want to change, and then enter a new name and the cell renames itself what the user entered. I am currently using the built-in settings application to save link data.

I do not need to know every aspect of what I just asked. I just want to know if the user can edit the text of the table cell and what methods I will use for this.

I have seen other issues that cover similar foundations, but usually from a more programmatic basis.

+7
source share
3 answers

The user cannot directly edit the text of a table cell. (Technically it will be cell.textLabel.text). However, if they go into edit mode, you can easily display a UITextField in a cell (or in a modal form) that is pre-populated with the current value, allows you to edit, save and update cell.textLabel. text value.

+7
source

This is where UITableView cells are created, usually

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

(one of the delegates for a UITableView ).

So, if you subclass UITableViewCell and set the text property, you can do what you want, including holding an instance of UITextField in the cell. Make sure you use dequeue material as usual.

Then, when the user touches UITableViewCell , you can highlight the UITextField focus:

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.thatTextFieldIMentioned becomeFirstResponder]; } 

If you subclass UITableViewCell a UITextFieldDelegate and make it a delegate for a text field, you can easily handle these annoyingly hard-to-catch methods:

 - (void)textFieldDidEndEditing:(UITextField *)textField {  NSLog(@"yeah inform someone of my change %@", textField.text); } - (BOOL)textFieldShouldClear:(UITextField *)textField {  return YES; } - (BOOL)textFieldShouldReturn:(UITextField *)textField {  [textField resignFirstResponder];  return YES; } 
+6
source

So, I think the best way is to use tabelview cell editstyle. You can use setview for the setview delegate. You can do something you want when the user edits the cell. Set the cell style to:

 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath 

Do when the user clicks the edit button in:

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
-one
source

All Articles