lingguang1997
lingguang1997

Reputation: 443

How to implement "swipe to delete" a custom UITableViewCell in a UITableView which is not in an UITableViewController?

Can someone provide the source code to do that? I set the data source and delegate and tried

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //remove the deleted object from your data source.
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

but it's not working. I don't want to add UIGestureRecognizer to the cells if possible.

Appreciate if someone gives suggestion.

Thank you very much!

Upvotes: 2

Views: 1079

Answers (1)

koray
koray

Reputation: 242

Make sure your Controller in your .h file is implementing UITableViewDelegate protocol, and set your tables datasource to that Controller. try this and let me know if those functions get called.

example...

SomeController.h file

@interface SomeController : UIViewController <UITableViewDataSource>
....

SomeController.m file

....

    myTableView.dataSource = self;

....

-GoodLuck!

Upvotes: 1

Related Questions