【问题标题】:How to get indexpath of selected row on longgesture press in UITableView in xamarin.ios如何在 xamarin.ios 的 UITableView 中获取长按时所选行的索引路径
【发布时间】:2017-01-17 13:59:47
【问题描述】:

我正在开发 xamarin.ios。我创建了一个 UITableView 并在其中绑定了一些数据。现在我想在表格的 UITableViewCell 上使用 LongPressGesture。我在 LongPressGesture 上打开一个弹出窗口,并希望显示与所选行相关的数据。但我不知道如何在 LongPress 上获取选定行的索引路径。 请更新如何获取长按手势的 indexpath 值?

【问题讨论】:

    标签: ios xamarin xamarin.ios


    【解决方案1】:

    将 UILongPressGestureRecognizer 添加到 TableView,然后将此代码用作您的操作。

    void HandleLongPress (UILongPressGestureRecognizer longPressGesture)
    {
        var point = longPressGesture.LocationInView (yourTableView);
    
        var indexPath = yourTableView.IndexPathForRowAtPoint (point);
    
        if (indexPath == null)
        {
            System.Console.WriteLine ("Long press on table view, not row.");
        }
        else if (longPressGesture.State == UIGestureRecognizerState.Began)
        {
            System.Console.WriteLine ($"Long press on row, at {indexPath.Row}");
        }
    }
    

    这应该可行。

    【讨论】:

      【解决方案2】:

      在 .xaml.cs 文件中添加以下方法,您将在长按选择时获得所选行的 indexPath

      public override void RowSelected (UITableView tableView, NSIndexPath indexPath) { UIAlertController okAlertController = UIAlertController.Create ("Row Selected", tableItems[indexPath.Row], UIAlertControllerStyle.Alert); okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null)); ...

      tableView.DeselectRow (indexPath, true);
      

      }

      【讨论】:

        猜你喜欢
        • 2020-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多