Change Table to Edit Mode and Delete Rows Inside a Normal Viewcontroller

change Table to Edit mode and delete Rows inside a normal ViewController

I could just solve the problem for deleting the rows. To make it work i inserted like ACB told me, the following methods:

//when blue accessory button has been pressed
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{

//turn into edit mode
[tableView setEditing:YES animated:YES];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated{

[super setEditing:editing animated:animated];
}

// method to enable the deleting of rows
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

if (editingStyle == UITableViewCellEditingStyleDelete) {

Activity *activity = [allActivities objectAtIndex:indexPath.row];

// remove the item from the array
[allActivities removeObjectAtIndex:indexPath.row];

//delete element from database with created method
[dataController deleteFromTable:@"activity" theElementWithID:activity._id];

[tableView setEditing:NO animated:YES];

// refresh the table view
[tableView reloadData];
}

}

I found this solution on cocoapi.wordpress.com/tag/caneditrowatindexpath

But i still have just one problem. If somebody enters in editing mode, and won't delete a row, there is no possibility, to turn the editing mode off again. If you switch the view and come back again it autmatically stops, but otherwise its not possible because i don't have a NavigationController inserted.

Is there a way, to access the state of the Deletion Control on the left of the row while editing? it would be useful to detect if somebody turned the deletion off again to jump out of editing mode.

It might not fit the apple user guide, but im just on my first project and it should just work.

How can I delete selected row from UITableView?]

Try like below lines:

[bookmarks removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];

bookmarks is my array name that I load on table view cell you should give your array name in place of this.

How to add extra cells in a UITableView in editing mode?

Where did you enter the editing state of the tableView? You should do it in -[UINavigationController setEditing:animated:]:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];

if(editing){
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationBottom];
} else {
// delete section
}
}

So instead of calling setEditing on the tableView, call it on the navigationController (probably self) The controller will then handle the editing mode.

So the controller has an editing mode, and the tableView has. The tableView will be in editing mode once you set it programmatically, or when the user swipes to delete a row. In the latter case, the controller is not in editing mode, but the tableView is.

UITableView: after deleting rows and adding new ones, new rows are not selectable

Finally found it - thanks to @Rory McKinnel for making me look closer at my custom cell implementation. It was doing virtually nothing because I'd stripped the guts out of it during my debugging efforts, but crucially it was still overriding prepareForReuse and was missing a call to super. Ouch.

UITableViewCell, show delete button on swipe

During startup in (-viewDidLoad or in storyboard) do:

self.tableView.allowsMultipleSelectionDuringEditing = false

Override to support conditional editing of the table view. This only needs to be implemented if you are going to be returning NO for some items. By default, all items are editable.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
}
}

How to bring the Edit functionality

You have to asign the following button in the - (void)viewDidLoad

self.navigationItem.rightBarButtonItem = self.editButtonItem;

and then implement the following methods:

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}

Different ViewController called when TableView is Editing

When you push your view controller in the didSelectRowAtIndexPath, you can check if the tableview is in editing mode. If it is, you can push a different view controller.

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...stuff that doesnt matter...
if(aTableView.editing) // The tableview is in editing mode
[[self navigationController] pushViewController:otherViewController animated:YES];
else [[self navigationController] pushViewController:detailViewController animated:YES];

}

editing is a property that returns YES if the tableview is currently in editing mode.



Related Topics



Leave a reply



Submit