Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After a swipe left on a UITableViewCell in a UITableView, how do I programmatically close the swipe?

Tags:

xcode

ios

ios8

I enable swipe left on a row to show a action. Upon tapping on the action, I want the swiped open table cell to close. How do I go about doing that?

I've tried to cell.setEditing(false, animated:true) but setting editing doesn't close the swipe.

like image 903
Sam Avatar asked Oct 29 '14 06:10

Sam


People also ask

How do you customize swipe edit buttons in UITableView?

As of iOS 8.0 there's an easy way to customize the list of buttons that appear when the user swipes from right to left: editActionsForRowAt . Return an array of UITableViewRowAction objects that have titles and styles (and also background colors if you want to customize their appearance), and iOS does the rest.

How do I swipe to delete Uitableviewcells?

When you want to handle deleting, you have to do three things: first, check that it's a delete that's happening and not an insert (this is down to how you use the UI); second, delete the item from the data source you used to build the table; and third, call deleteRows(at:) on your table view.

What is IndexPath UITableView?

IndexPath contains information about which row in which section the function is asking about. Base on this numbers you are configuring the cell to display the data for given row.

How do I populate UITableView?

There are two main base ways to populate a tableview. The more popular is through Interface Building, using a prototype cell UI object. The other is strictly through code when you don't need a prototype cell from Interface Builder.


1 Answers

Note: I don't have enough reputation to comment on your question, therefore I would have to make some assumptions regarding your implementation.

If you are trying to close the cell in tableView:commitEditingStyle:forRowAtIndexPath: you can try this:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // remove delete button only after short delay
        [self performSelector:@selector(hideDeleteButton:) withObject:nil afterDelay:0.1];
    }
}

- (void)hideDeleteButton:(id)obj
{
    [self.tableView setEditing:NO animated:YES];
}

The reason for using performSelector:withObject:afterDelay: is the note in the Table View Programming Guide for iOS

Note: The data source should not call setEditing:animated: from within its implementation of tableView:commitEditingStyle:forRowAtIndexPath:. If for some reason it must, it should invoke it after a delay by using the performSelector:withObject:afterDelay: method.

All this was heavily inspired by the following answer: https://stackoverflow.com/a/22063692/2433921

like image 186
Georgi Avatar answered Sep 16 '22 12:09

Georgi