开发者

Imitating UITableViewControllers behavior for tableView:willBeginEditingRowAtIndexPath: on UIViewController

In my app I needed to use a UIViewController to control my tableView instead of a UITableViewController (because I needed an additional view beneath my tableView). I've implemented the view controller (and delegate and datasource) succesfully but now I need to get the right behavior on my UITableViewCells when the user swipes from left to right on a cell.

The behavior needed when the user taps the edit button (display the rearrangement drag handle, and the little "-" button that makes the delete confirmation show) was pretty easy to do. I just did like this in my view controller:

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

But I can't figure out how to make the delete confirmation show if the user swipes from left to right on the cell. This is what I've got in my tableView:willBeginEditingRowAtIndexPath: right now:

- (void)tableView:(UITableView *)aTableView willBeginEditingRowAtIndexPath:(NSIndexPath *)index开发者_如何学运维Path {
 // Tell the cell to do its custom animations
 [[self.tableView cellForRowAtIndexPath:indexPath] setEditing:YES animated:YES];

 // set the viewControllers editing to YES, thus changing the editButton to a doneButton
 self.editing = YES;
}

With this code the editButton changes to a doneButton as it's supposed to and the cell does its animations to make room for the delete confirmation, but the delete confirmation itself doesn't show.

My question: How do I show the delete confirmation on a specified tableviewCell?


Make sure that you implement the method:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

when this is implemented (it needs to be anyway, since this is where you handle the delete button), the swipe a row to show the delete button gesture is implemented for you automatically.


I figured it out myself. Actually all i had to do was insert the same code from my setEditing:animated: into the tableView:willBeginEditingRowAtIndexPath method.

- (void)tableView:(UITableView *)aTableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
   [super setEditing:isEditing animated:animated]; 
   [self.tableView setEditing:isEditing animated:animated];

   //Self.editing handles the done / edit button
   self.editing = YES;
}

That did the trick. I don't know how the tableView knows that it's only supposed to show the delete button for one cell, but it does and it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜