Reputation: 532
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if(editingStyle == UITableViewCellEditingStyleDelete){
//[theSimpsons removeObjectAtIndex:indexPath.row];
NSString *time = [[arrayList objectAtIndex:indexPath.row] objectForKey:@"TIME"];
NSString *date= [[arrayList objectAtIndex:indexPath.row] objectForKey:@"DATE"];
DBManager *dbm = [[DBManager alloc] init];
[dbm initiallzeDatabase];
[dbm deleteItemAtIndexPath:time :date];
//arrayList = [dbm getParkResults];
//[parkTableView reloadData];
[arrayList removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
I have used the above code in my app. The editing part works fine, but the thing is, the delete button appears only when we swipe from left-to-right on a cell. Now i have a menu that opens on left-to-right swipe like Facebook. So how can i make sure that the delete button appears when the user swipes from right-to-left.
Upvotes: 6
Views: 6841
Reputation: 47049
You can got it by using two Methods of UITableVie.
// This only needs to be implemented if you are going to be returning NO
// for some items. By default, all items are editable.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
//When you remove a cell of your tableview, you also has to remove your array object at index x
}
}
After removing the object. You have to reload the UITableView
.
[self.tableView reloadData];
For More Information Read This OfficialDocumentaion.
Upvotes: 1
Reputation: 4750
Please dont set the delegate to UISwipeGestureRecognizer
Write this in viewDidLoad
or where you have initiated your UITableView
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(callYourMethod:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.tblView addGestureRecognizer:swipeRight];
No need to write anything in the below method.
- (void)callYourMethod:(UISwipeGestureRecognizer *)recognizer
{
}
Upvotes: 6
Reputation: 309
This might work. Put this code in awakeFromNib
or init
:
UISwipeGestureRecognizer *swipeDelete = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeDelete:)];
[swipeDelete setDirection: UISwipeGestureRecognizerDirectionLeft];
[yourTableView addGestureRecognizer:swipeDelete];
then in handleSwipeDelete
organize your cell and show delete button. You will probably need to handle cell state and change it on swipeLeft/swipeRight.
Upvotes: 2
Reputation: 104
looks like you need to customize the behaviour of UITableViewCell, I have never noticed that Apple has supplied the behaviour after swiping right to left....
Upvotes: 1