Monotouch “Swipe To Delete” CommitEditingStyle Not

2019-08-15 17:21发布

问题:

I am trying to implement "swipe to delete" functionality in a uitableview. I have followed other tutorials/forums and extended UITableViewSource. Within that class I have overridden the following methods:

public override void CommitEditingStyle(UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath)
{
   if (editingStyle == UITableViewCellEditingStyle.Delete)
   {
       tableItems.RemoveAt(indexPath.Row);
       tableView.DeleteRows(new [] { indexPath }, UITableViewRowAnimation.Fade);
   }
}

public override UITableViewCellEditingStyle EditingStyleForRow(UITableView tableView, NSIndexPath indexPath)
{
    return UITableViewCellEditingStyle.Delete;
}

public override bool CanEditRow(UITableView tableView, NSIndexPath indexPath)
{
    return true;
}

I believe these are the only methods I should need. However when I perform a swipe gesture, the CanEditRow and EditingStyleForRow methods are called but CommitEditingStyle is never called. Any help would be much appreciated. Thanks.

回答1:

I'm answering my own question. It turns out my listview was larger than its container so the delete buttons were simply hidden offscreen. Face palm.

sblom's knowledge about which events get fired when helped me a lot. Good info.



回答2:

To the best of my knowledge, you only have to override CommitEditingStyle in order to make this work. As long as you're seeing a delete button appear when you swipe one of your rows, you have a very strong indication that all of the tricky parts are working. If you're not seeing a delete button appear when you swipe, we should focus on figuring out why it's AWOL.

Technically, CanEditRow is on the UITableViewDataSource protocol and EditingStyleForRow is on UITableViewDelegate, so if both of those are indeed being called, we can rule out problems such as your UITableViewSource only being assigned to DataSource or Delegate instead of to the combined Source property.



标签: xamarin.ios