I read about table view and it's editing styles but I have some problems as there is only three editing style as follow:
UITableViewCellEditingStyleNone
UITableViewCellEditingStyleDelete
UITableViewCellEditingStyleInsert
I want to have reordering the tableview cells which I implemented successfully using it's delegates.
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//handle the editing style
}
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
//move cells
}
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
I don't want to use UITableViewCellEditingStyleDelete
as it shows a red circular button on table view. Instead of this I want swipe-to-delete and the reordering functionality together.
Is there any way to implement this?
This can be done but with the following conditions. The swipe-to-delete will only work while the table view is not in edit mode and the table re-ording will only work while the table view is in edit mode. You will not be able to have swipe-to-delete work at the same time as table re-ordering.
To make this work you need the following:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
// Only allow deletion when the table isn't being edited
return tableView.isEditing ? UITableViewCellEditingStyleNone : UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// handle the row deletion
}
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
// Check if move is valid
return proposedDestinationIndexPath;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView * nonnull)tableView moveRowAtIndexPath:(NSIndexPath * nonnull)fromIndexPath toIndexPath:(NSIndexPath * nonnull)toIndexPath {
// process the moved row
}
You will need the standard Edit button in the navigation bar (or some other way to toggle the table's edit mode). A common way to do this is to add the following line inside your table view controller's viewDidLoad
method:
self.navigationItem.rightBarButtonItem = [self editButtonItem];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With