Casey Hancock
Casey Hancock

Reputation: 508

Returning an empty array of UITableViewRowAction in EditActionsForRow breaks swiping on a table cell

I have a UITableView and I want to have some of the rows swipeable, and some not, but I want them all to be able to be selected via selection bubbles. The problem is, CanEditRow controls whether or not you can swipe a cell and whether or not you can select it via it's bubble.

So what I tried to do to get around this is: in EditActionsForRow just return a an empty UITableViewRowAction[] when I don't want the cell to be swipeable, and one with the appropriate UITableViewRowActions when I do want it to be swipeable. The problem is, as soon as I hit the case where the empty UITableViewRowAction[] array is returned, swiping no longer works.

One hack I tried was to return an array with just one UITableViewRowAction in it that has no text and does nothing. The only problem is that the Table Cell doesn't "bounce back" the same way it does the first time you try to swipe a cell that can't be swiped.

Does anyone have experience with this problem or know of a workaround?

Upvotes: 0

Views: 526

Answers (1)

Andrius
Andrius

Reputation: 2372

If I understand your problem correctly, CanEditRow must always return true (for selection bubbles to work) and only some cells should have row action buttons.

The solution would be to override EditingStyleForRow method, check which row is about to be edited and return UITableViewCellEditingStyle.None if you do not want to add action buttons.

Example implementation of UITableViewSource (every second cell has row action buttons):

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

public override UITableViewCellEditingStyle EditingStyleForRow (UITableView tableView, NSIndexPath indexPath)
{
    if(indexPath.Row % 2 == 0) // dummy condition
        return UITableViewCellEditingStyle.Delete; 
    else 
        return UITableViewCellEditingStyle.None;
}

public override UITableViewRowAction[] EditActionsForRow (UITableView tableView, NSIndexPath indexPath)
{
    UITableViewRowAction hiButton = UITableViewRowAction.Create (UITableViewRowActionStyle.Default, "Hi", delegate {});

    return new UITableViewRowAction[] { hiButton };
}

Upvotes: 4

Related Questions