UITableViewCell 在滚动过程中突出显示,然后稍后选择

UITableViewCell getting highlighted during scrolling, then selected later

我有一个带有 UITableView 的 Xamarin.iOS 应用程序。视图中只有一个部分。如果我将手指放在 table 的一行(例如,第 5 行)上,按住它一会儿,然后滚动 table 视图,该行将突出显示。这有点不受欢迎,但没什么大不了的。但是,如果我然后点击另一行(比如第 10 行)到 select 它,我的 UITableViewSource 对象将获得一个带有最初触摸的行的 RowSelected 回调。这是个大问题,因为应用程序现在认为用户 select 编辑了错误的行。此外,我看不到任何随点击行正确到达的回调。

我试图通过在滚动结束时去除select该行来解决这个问题。没用。

我有一个非 Xamarin 版本的应用程序,它没有表现出这种行为。

这是我的一些代码(来自自定义 UITableViewSource 子类)和输出:

public int HighlightedRowIndex {get;set;} = -1;

public override void RowHighlighted(UITableView tableView, NSIndexPath rowIndexPath) {
  // I actually don't want to be looking at highlighted at all; I'm just going here to try to understand/fix the problem.
  CommonDebug.LogLine("row highlighted", (int)rowIndexPath.Row);
  this.HighlightedRowIndex = rowIndexPath.Row;
}

public override void DraggingEnded(UIScrollView scrollView, bool willDecelerate) {
  int highlighted = this.HighlightedRowIndex;
  if (highlighted >= 0) {
    NSIndexPath indexPath = NSIndexPath.FromRowSection(0, (nint)highlighted);
    UITableView tableView = scrollView as UITableView;
    if (tableView!=null) {
      CommonDebug.LogLine("ATTEMPTING HACK FIX, BUT IT DOES NOT HELP");
      //  tableView.SelectRow(indexPath, false, UITableViewScrollPosition.None); // This can be commented out, or not; it doesn't help either way.
      tableView.DeselectRow(indexPath, false);
    }
  }
  this.HighlightedRowIndex = -1;
}

public override bool ShouldHighlightRow(UITableView tableView, NSIndexPath rowIndexPath) {
  CommonDebug.LogLine("should highlight", (int)rowIndexPath.Row);
  // Returning false from here would prevent the problem, but would also prevent any selection . . .
  return true;
}

public override void RowSelected(UITableView tableView, NSIndexPath indexPath) {
  CommonDebug.LogLine("IOSListViewSource selected", indexPath.Row);
  if (this.SelectIgnoreCount == 0) {
    ListViewSectionModel model = this.GetModel();
    int row = (int)indexPath.Row;
    model.SelectItem(row);
  } else {
    CommonDebug.LogLine("ignoring");
  }
}

输出:

我将手指放在第 5 行,让它在那里停留片刻,然后滚动 table 视图:

should highlight 5
row highlighted 5
ATTEMPTING HACK FIX, BUT IT DOES NOT HELP

然后我点击第 10 行:

WillSelectRow 5
IOSListViewSource selected 5

没有迹象表明我发现第 10 行已被点击。

有没有人遇到同样的问题/找到解决方法?

已修复:

public int HighlightedRowIndex {get;set;} = -1;

public override void DraggingEnded(UIScrollView scrollView, bool willDecelerate) {
  int highlighted = this.HighlightedRowIndex;
  if (highlighted >= 0) {
    UITableView tableView = scrollView as UITableView;
    if (tableView!=null) {
      tableView.ReloadData();
    }
  }
  this.HighlightedRowIndex = -1;
}

public override void RowHighlighted(UITableView tableView, NSIndexPath rowIndexPath) {
  this.HighlightedRowIndex = rowIndexPath.Row;
}

类似的方法是将自定义委托添加到 UITableView(List)。我这样做是因为在我的情况下子类化 UITableViewSource 是个坏主意。

定义自定义委托并覆盖滚动函数:

public class CustDelegate : UITableViewDelegate
 {
    private CustomListViewRenderer list { get; set; }
    public CustDelegate(CustomListViewRenderer listRenderer)
        : base()
    {
        list = listRenderer;
    }
    public override void Scrolled(UIScrollView scrollView)
    {
        if(list != null)
            list.Scrolled();
    }
}

在 ListRenderer 中向 UITableView 添加委托

(UITableView)Control.Delegate = new CustDelegate(this);

并定义防止突出显示的 Scrolled 函数。

希望这对某人有用。