0

I'm working on an iOS app using Xamarin and MVVM Cross.

I have a table view from which one row can be selected at a time, which is indicated using the Checkmark accessory on the UITableViewCell. I'm essentially following the answer from this: ✔ Checkmark selected row in UITableViewCell

The problem is that Reloading the table data (or even reloading just the two rows that changed) seems to interrupt the animation of De-selecting the row. I would like it to fade out nicely. For example, on an iPad, the behavior in Settings -> Notes -> Sort Notes By is exactly what I want. Touching a row highlights it, letting go moves the checkmark, and then the highlight fades out.

After messing with this for several hours, the only solution I found was to Reload the rows to update the checkmark, then manually Re-select and De-select the row. This works fine, but feels like a big hack. Is there a cleaner way to do this?

Here's the relevant snippets:

public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
    SetCellCheckmark(tableView, indexPath);
    base.RowSelected(tableView, indexPath); //This handles de-select via the MvxTableViewSource DeselectAutomatically property
}

private void SetCellCheckmark(UITableView tableView, NSIndexPath newSelection)
{ 
    if (newSelection == null)
        return;

    var rowsToUpdate = new NSIndexPath[_checkedRow == null ? 1 : 2];
    rowsToUpdate[0] = newSelection;
    if (_checkedRow != null)
        rowsToUpdate[1] = _checkedRow;

    _checkedRow = newSelection;
    tableView.ReloadRows(rowsToUpdate, UITableViewRowAnimation.None);

    tableView.SelectRow(newSelection, false, UITableViewScrollPosition.None); //This feels like a hack
}

private UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
{
    var cell = TableView.DequeueReusableCell(MyCellType, indexPath);
    if (cell == null)
        return null;

    if (indexPath == _checkedRow)
    {
        cell.Accessory = UITableViewCellAccessory.Checkmark;
    }
    else
    {
        cell.Accessory = UITableViewCellAccessory.None;
    }

    return cell;
}

If you have an answer in Objective-C, I could probably translate it into Xamarinland.

Community
  • 1
  • 1
J Nelson
  • 138
  • 1
  • 1
  • 12
  • Why are you reloading the rows? – Cheesebaron Jul 08 '16 at 12:08
  • If I just update the accessory, the cellviews do not redraw. So reload seems to be necessary. My version is an optimization from http://stackoverflow.com/questions/7982944/checkmark-selected-row-in-uitableviewcell which reloads the entire table -- instead i only reload the 2 cells that changed. – J Nelson Jul 11 '16 at 15:56
  • I've never had to do that. You just need to set the checkmark on the cell itself instead of adding the index of the cell to a list and then reload the table data. – Cheesebaron Jul 11 '16 at 17:55

0 Answers0