Entering Editing Mode via swipe


#1

Hi,

I noticed that when you delete a row by “Swiping” - the “Add New Item…” row still gets added even though it isn’t operational.

Implementing willBeginEditingRowAtIndexPath and didEndEditingRowAtIndexPath as No Op’s seems to do the trick.

Can’t admit to fully understanding why but it does the job !

Gareth


#2

You are right.

Just tried it on my own and “Add New Item…” appears at the bottom, but without the plus icon, so it just sits there useless.

Unfortunately, -setEditing:animated: can’t tell if it was called because of the swipe or because the Edit bar button was pressed. So I resurrected -editBtnPressed: method from the earlier chapters and attached it to the bar button in -init.

[code] [[self navigationItem] setLeftBarButtonItem:self.editButtonItem];

[[self navigationItem].leftBarButtonItem setAction:@selector(editBtnPressed:)];

[self navigationItem].title = @“Homepwner”;
[/code]

Then I added a flag that tells me if we ended up editing because the bar button was pressed:

[code]@interface ItemsViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate> {
NSMutableArray *possessions;
BOOL barBtnEditingFlag;
}
@property (nonatomic, retain) NSMutableArray *possessions;

  • (void)editBtnPressed:(id)sender;
    [/code]
    Here is the method:

[code]- (void)editBtnPressed:(id)sender
{
NSLog (@“editBtnPressed: %@”, sender);

if ([self isEditing]) {
[self setEditing:NO animated:YES];
}
else {
barBtnEditingFlag = YES;
[self setEditing:YES animated:YES];
}
}[/code]
And here’s new version of -setEditing: animated:

[code]- (void)setEditing:(BOOL)flag animated:(BOOL)animated
{
NSLog (@“setEditing:(%d) animated:”, (int)flag);
[super setEditing:flag animated];

NSIndexPath *idxPath = [NSIndexPath indexPathForRow:[self.possessions count] inSection:0];
NSArray *paths = [NSArray arrayWithObject:idxPath];

if (flag) {
if (barBtnEditingFlag)
[[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationLeft];
}
else {
if (barBtnEditingFlag)
[[self tableView] deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
barBtnEditingFlag = NO;
}
}[/code]
And then tableView:numberOfRowsInSection:

[code]- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
NSLog (@“tableView:numberOfRowsInSection: %d”, section);
NSInteger rows = [self.possessions count];

if ([self isEditing] && barBtnEditingFlag)
rows++;

return (rows);
}[/code]

And now it seems to be working as expected. And sorry if the code above is too different than what’s in the book. I always change minor stuff (like method and variable names, remove repeating lines) as I type examples from books. This keeps me concentrated and makes sure I fully understand what’s going on.


#3

Nice one.

I ran into further problems with my approach:

If I swiped to bring up the delete button and then pressed the Edit button it was still adding the “Add New Item…” row

I introduced a “swiped” flag that I set in the willBeginEditingRowAtIndexPath and then pretty much did the same as you in the setEditing and numberOfRowsInSection methods.

All works ok but my Edit button still says Edit after the swipe whereas yours says Done (better).

Gareth


#4

Have updated the implementation of ItemsViewController to handle swipe deletes.
http://forums.bignerdranch.com/viewtopic.php?f=53&t=174&sid=01d932026f512f3a8eb323e0a19bb3ab


#5

It took me a while to understand what did you say hare… but I finally get it. My bad. You said you’re not satisfied with your solution, so I wasn’t really trying to understand why.

So, basically, what you say is that when you swipe, red Delete button appears, but bar button still shows Edit. And if you press the Edit button, strange things happen.

Well, I think I found the solution now. It is all well described in the header files UITableView.h and UIViewController.h, but for some reason I haven’t seen it before. As I said in a sibling thread just yesterday, my previous solution wasn’t really good because there’s the proper way to distinguish between swipe editing and bar button editing.

From the header file:

And today I found this in UIViewController.h:

So, in the end, what is needed is something like this:

[code]- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
swipeEditingFlag = YES;
[self setEditing:YES animated:YES];
}

  • (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
    {
    [self setEditing:NO animated:YES];
    swipeEditingFlag = NO;
    }[/code]
    Or, just call [super setEditing:YES animated:YES ]; It has the same effect, and it should because our method shouldn’t do much when swipeEditingFlag is set, but calling this method on super just doesn’t feel right here. That’s why I think this version is better.

And I think now we have it all covered.