Swipe to delete vs. edit button tapped on iPhone
I set up my viewController to use the edit button in the way Apple recommends:
self.navigationItem.rightBarButtonItem = self.editButtonItem;
When the user taps the edit button, it causes the setEditing:animated:
method to trigger. In this method, I add or remove a "new row" dependi开发者_运维知识库ng on the editing
value (i.e. a row the user can tap to add a new item).
When the user swipes across a cell (while not in edit mode), it also calls the setEditing:animated:
, which causes my code to add this "new row" incorrectly. It should only show this new row when the entire viewController is in edit mode (i.e. when the edit button is tapped).
How can I resolve this issue?
If you implement tableView:willBeginEditingRowAtIndexPath:
and tableView:didEndEditingRowAtIndexPath:
, the behavior of the method calls will change...
It will no longer call setEditing:animated:
, you must handle all editing change logic inside the above two methods. As a side effect of this, self.editing
will no longer be YES
when a swipe gesture is invoked. This is because the setEditing:animated:
is responsible for setting self.editing = YES
.
Use the UITableViewDelegate
method tableView:willBeginEditingRowAtIndexPath:
. From the documentation:
This method is called when the user swipes horizontally across a row; as a consequence, the table view sets its editing property to
YES
(thereby entering editing mode) and displays a Delete button in the row identified by indexPath. In this "swipe to delete" mode the table view does not display any insertion, deletion, and reordering controls. This method gives the delegate an opportunity to adjust the application's user interface to editing mode. When the table exits editing mode (for example, the user taps the Delete button), the table view callstableView:didEndEditingRowAtIndexPath:
.
In tableView:willBeginEditingRowAtIndexPath:
, set a flag that editing mode was triggered using swipe to delete. Then, in setEditing:animated:
, check the flag to see if editing mode was triggered normally or using swipe to delete and perform some action based on that check. Lastly, reset the flag in tableView:didEndEditingRowAtIndexPath:
so that the default action will be performed when the edit button is pressed.
I have no idea what your "new row" is about, but you could attach your edit button to a helper method that sets a flag before calling setEditing, and there you can check if the said flag is set and behave accordingly. Then clear the flag at the end of the method.
Senseful u have to override setEditing:animated: method. or even u can set flag in this method as Eiko has suggested.
or other way is
u implement custom naviagtion bar. in that case u need image of Navigation bar and 2 buttons above it. add target to that button and implement functionality as your need
The table enters edit mode both when the user taps the edit button and also when swiping on a cell. To differentiate between these two cases you can override willBeginEditingRowAtIndexPath
(remember to call super or editing mode won't be entered) and store the index path in a property. Override setEditing
and you can check the property and only add your insert row if it is nil (i.e. when the user isn't swiping).
@interface MasterViewController ()
@property (nonatomic, strong, nullable) NSIndexPath *tableViewEditingRowIndexPath;
@end
@implementation MasterViewController
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath{
self.tableViewEditingRowIndexPath = indexPath;
[super tableView:tableView willBeginEditingRowAtIndexPath:indexPath]; // calls setEditing:YES
}
-(void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath{
[super tableView:tableView didEndEditingRowAtIndexPath:indexPath]; // calls setEditing:NO
self.tableViewEditingRowIndexPath = nil;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
if(self.tableViewEditingRowIndexPath){
// we are swiping
return;
}
if(editing){
// insert row for adding
}
else{
// remove row for adding
}
}
精彩评论