如何在ios UITableView中获得平滑的选择/取消选择动画

时间:2016-07-06 15:13:13

标签: ios uitableview xamarin.ios uikit mvvmcross

我正在使用Xamarin和MVVM Cross在iOS应用上工作。

我有一个表视图,一次可以选择一行,使用UITableViewCell上的Checkmark附件指示。我基本上遵循了这个答案:✔ Checkmark selected row in UITableViewCell

问题是重新加载表数据(甚至只重新加载已更改的两行)似乎会中断取消选择行的动画。我希望它很好地淡出。例如,在iPad上,“设置”中的行为 - >备注 - >排序笔记正是我想要的。触摸一行突出显示它,放开复选标记,然后突出显示淡出。

在弄乱了几个小时后,我找到的唯一解决方案是重新加载行以更新复选标记,然后手动重新选择并取消选择该行。这很好,但感觉就像一个大黑客。有更清洁的方法吗?

以下是相关摘要:

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;
}

如果您在Objective-C中有答案,我可能会将其翻译成Xamarinland。

0 个答案:

没有答案