Allow a user to edit the text in a UITableView cell - objective-c

I am creating a "favorite pages" menu in my tableview application, where users can save their favorite web pages and then navigate to them easier through the menu.
For ease of remember which link is which, I want to allow the user to click a button that says "edit," somehow select the cell that they want to edit the name of, and then type in the new name and have the cell rename itself to what the user typed in. I am currently using the built in settings application to save the link data.
I don't need to know every aspect of what I just asked. I just want to know if it is possible for the user to edit the cell text of a tableview, and what methods I would use to do so.
I've seen other questions that cover similar ground, but generally from a more programmatic basis.

The user cannot directly edit the cell text of a tableview. (Technically, it would be the cell.textLabel.text). However, if they go into an "edit" mode, you could easily display a UITextField in the cell (or in a modal view) which is prepopulated with the current value, let them edit it, save and then update the cell.textLabel.text value yourself.

Your UITableView cells are created here, usually
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
(one of the delegates for the UITableView).
So if you subclass UITableViewCell and expose a text property, you can do what you want, including holding a UITextField instance in the Cell. Make sure to use the dequeue stuff as you would normally.
Then, when the user touches the UITableViewCell you can give the UITextField the focus:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.thatTextFieldIMentioned becomeFirstResponder];
}
If you make the UITableViewCell subclass a UITextFieldDelegate and make it the delegate for the text field, you can easily handle these annoyingly hard-to-remember 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;
}

So,I think the best way is use tabelview cell editstyle.
You can use tableview delegate set cell editstyle. You can do something you want when user edit the cell.
Set cell style in:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
Do things when user click edit button in:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

Related

Make 'edit' button trigger edit-mode on UITableView

This might be a very stupid question, but I have very little knowledge about Xcode, Objective-C and iOS development in general.
What I have is a UITableViewController, with a number of cells. I've gotten the 'add' and delete (swipe to delete) functions to work, but I cannot figure out how to connect the 'edit' button to a function that will trigger the editing mode of that TableView, so I can rearrange the order of the elements.
I have un-commented these functions:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
XYZToDoItem *itemToMove = [self.toDoItems objectAtIndex:fromIndexPath.row];
[self.toDoItems removeObjectAtIndex:fromIndexPath.row];
[self.toDoItems insertObject:itemToMove atIndex:toIndexPath.row];
[self.tableView reloadData];
}
As you can see I have also made a few additions to the moveRowAtIndexPath method following the documentation.
In addition there are these functions;
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[self.toDoItems removeObjectAtIndex:indexPath.row];
[self.tableView reloadData];
}
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
As previously stated the "swipe to delete" function works, I don't know how, but I simply just uncommented these last two methods and edited one a bit to fit my code.
To be more specific, what I'm asking is how can I link the 'edit' button I have in my Navigation Controller to trigger the edit mode for the TableView, thereby showing the three lines (?) known from the TableViews editing mode (and letting me drag the elements to an order I want).
Thanks in advance for all your help! :)
If this is a UITableViewController you have two options. Either use the built-in edit button, which will give you the automatic Edit/Done button change. Use self.editButtonItem in your navigation bar:
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
Or you could use setEditing:animated to change editing state of the tableViewController:
// self is a UITableViewController
[self setEditing:YES animated:YES];
If your viewController is not a subclass of UITableViewController you can set the editing mode of the tableView:
[self.tableView setEditing:YES animated:YES];
If you have a UITableViewController you should use the first way because it is most convenient.

How reorder row in customize UITableViewCell

So far I have customized my UITableViewCell with the icon reorder row.
I want my cell can be reordered when long press on the reorder icon and then move it.
The image below is what I have expected.
I have read many document they provided only long press on cell and move it but I don't want it because long press on cell I have my another gesture is renaming it; and also I don't want to use the edit/done of UITableView default from OS; so I decided to put reorder icon but I can not do, because I'm new in objective c.
Can anyone help by providing sample code here?
Thank for reading.
Override two methods of UITableViewDataSource
For reordering to work you will first need to bring the Table in editing mode. Which you can either do it from viewDidLoad or from a IBAction method.
-(void) viewDidLoad{
[super viewDidLoad];
self.myTableView.editing = YES;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath{
//Manipulate your data array.
}
By Default editing style is UITableViewCellEditingStyleDelete. So if you don't override the below method it will show both rearrange as well delete icon.
-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone;
}
Since, you have long gesture of your cell causes issue with the long gesture of the re-ordering of cell. You can add a UIView to the cell. Add all your elements in this UIView. Now, attach a UILongPress Gesture to this UIView (not to the cell.).

Cell at UISearchController SearchTableView not responding to didSelectRowAtIndexPath

I have a main table view with a search display controller. I'm using a custom cell.h which implement
(void)setSelected:(BOOL)selected animated:(BOOL)animated
When I select the cell from main table, the code above changes the background color of this selected cell. When I choose to search from the search bar, the search display controller display the selected cell in the correct background color using
(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
However, this cell which is displayed at the search display controller does not responds to didSelectRowAtIndexPath and didDeselectRowAtIndexPath. Other cells, on the other hand, responds to both methods.
What I want to achieve is a synchronization between the main table and the search table view at search display controller where selected cells should appear selected in both main table and search table view and yet allows selection and un-selection.
Please advice. Many thanks.
I found the answer to my question:
Use
(void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView
And
[tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:[searchResult indexOfObject:currentFriend] inSection:0] animated:NO scrollPosition:0];
Instead of
(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
Hope it helps!

Cannot make table re-order to work

From the UITableViewCell showsReorderControl docs:
For the reordering control to appear, you must not only set this property but implement the UITableViewDataSource method tableView:moveRowAtIndexPath:toIndexPath:. In addition, if the data source implements tableView:canMoveRowAtIndexPath: to return NO, the reordering control does not appear in that designated row.
I've got the both in my tableviewController :
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
NSLog(#"move from:%d to:%d", fromIndexPath.row, toIndexPath.row);
//just for test
}
My cell properties including re-order controls :
And yet I can't see the re-order control, what am I missing?
Have you put your UITableView into editing mode via
[tableView setEditing:YES animated:YES];
?
The first method you posted is needless, by the way, as that is the default behavior.
I think you need to actually get into edit mode for the table. Either try it in code when your view appears or create a button that will do it.
[tableView setEditing:YES animated:YES];

How to show deleteConfirmationButton on a clicked UITableViewCell

I'm trying to make an UITableViewCell that shows the deleteConfirmationButton (while in edit mode) just like happens when I click at the editing controls but in my case I want it when the user clicks over the UITableCell.
I've already set the property AllowsSelectionDuringEditing to YES and I can delete the rows, I just want the deleteConfirmationButton to avoid any accident.
Any tips on how to do it?
Thanks!!
To do this you would probably use the commitEditingStyleForRowAtIndexPath function, which is called when an object is selected or deleted during editing.
- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
And then for the confirmation do something like this:
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
//make a UIAlert and display confirmation, if yes then run the following code, if no then break
// remove the object
[myItems removeObjectAtIndex:indexPath.row];
// refresh the table view to display your new data
[tableView reloadData];
}