简体   繁体   English

UITableViewRowAction 在 Xamarin forms 上不起作用

[英]UITableViewRowAction not working on Xamarin forms

When i am trying to delete a row in table view by swiping left, Swipe is not working.当我试图通过向左滑动来删除表格视图中的一行时,滑动不起作用。 Sometimes after 10-12 attempts, table view row swipes and then i am able to see delete button.有时在 10-12 次尝试后,表格视图行滑动,然后我可以看到删除按钮。 Below is the code i am using.下面是我正在使用的代码。 Please let me know where i am doing it wrong?请让我知道我在哪里做错了?

Note: Issue is occurring on iPhone's.注意:问题出现在 iPhone 上。 On iPad's everything works fine.在 iPad 上一切正常。

using System;
using System.Collections.Generic;
using System.Linq;
using Project.Controls;
using Project.Events;
using Project.Models;
using Foundation;
using Prism.Autofac;
using Prism.Events;
using Prism.Ioc;
using UIKit;

namespace Project.iOS.Renderers
{
    public class ExtendedListViewMPSource : UITableViewSource
    {
        IList<PlaylistModel> tableItems;
        ExtendedListViewMP listView;

        public IEnumerable<PlaylistModel> Items
        {
            //get{ }
            set => tableItems = value.ToList();
        }

        public ExtendedListViewMPSource(ExtendedListViewMP view)
        {
            tableItems = (IList<PlaylistModel>)view.Items.ToList();
            listView = view;
        }

        /// <summary>
        /// Called by the TableView to determine how many cells to create for that particular section.
        /// </summary>
        public override nint RowsInSection(UITableView tableview, nint section)
        {
            return tableItems.Count;
        }

        /// <summary>
        /// Called by the TableView to get the actual UITableViewCell to render for the particular section and row
        /// </summary>
        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            // Request a recycled cell to save memory, if there are no cells to reuse, create a new one
            if (!(tableView.DequeueReusableCell(PlaylistViewCell.Key) is PlaylistViewCell cell))
            {
                cell = new PlaylistViewCell(PlaylistViewCell.Key);
                var views = NSBundle.MainBundle.LoadNib("PlaylistViewCell", cell, null);
                cell = ObjCRuntime.Runtime.GetNSObject(views.ValueAt(0)) as PlaylistViewCell;
            }

            var item = tableItems[indexPath.Row];
            cell.UpdateCellAsync(item);

            return cell;
        }

        public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
        {
            var model = tableItems[indexPath.Row];

            if (listView.ListViewSelectionMode == ExtendedListViewSelectionMode.Multiple)
            {
                model.Selected = !model.Selected;
                NSIndexPath[] indexPaths = { indexPath };
                tableView.ReloadRows(indexPaths, UITableViewRowAnimation.None);
            }

            listView.NotifyItemSelected(model);
            tableView.DeselectRow(indexPath, true);
        }

        public override UITableViewRowAction[] EditActionsForRow(UITableView tableView, NSIndexPath indexPath)
        {
            UITableViewRowAction deleteButton = UITableViewRowAction.Create(
                UITableViewRowActionStyle.Destructive,
                "Delete",
                    DeleteRowActionHandler
                ); 

            return new UITableViewRowAction[] { deleteButton };
        }

        private void DeleteRowActionHandler(UITableViewRowAction rowAction, NSIndexPath indexPath)
        {
            if (Xamarin.Forms.Application.Current is PrismApplication prismApplication)
            {
                var eventAggregator = prismApplication.Container.Resolve<IEventAggregator>();
                if (eventAggregator != null)
                {
                    var playlistId = tableItems[indexPath.Row].Id;
                    eventAggregator.GetEvent<PlaylistDeleteEvent>().Publish(
                        new PlaylistDeleteEventParameters(playlistId, true, listView.ItemPlaylistType)
                    );
                }
            }
        }
    }
}
 protected override void OnAppearing()
        {
            base.OnAppearing();

            if ((Application.Current.MainPage as MainMasterDetailPage) != null)
            {
                (Application.Current.MainPage as MainMasterDetailPage).IsGestureEnabled = false;
            }

        }

Now what if user doesn't navigate from Master Detail page.现在,如果用户没有从 Master Detail 页面导航怎么办。 and if page loads directly after Sign in如果页面在登录后直接加载

OnAppearing method and OnDisappearing will always fire when a page becoming visible or a page disappears.当页面变得可见或页面消失时, OnAppearing方法和OnDisappearing将始终触发。 Page loads by Navigate or loads directly after Sign in will both work.通过导航加载页面或在登录后直接加载都可以。

What you need to do is Disable it in the Page's OnAppearing method and enable it in OnDisappearing method as I mentioned in the comment.您需要做的是在页面的 OnAppearing 方法中禁用它,并在评论中提到的 OnDisappearing 方法中启用它。

protected override void OnAppearing()
{
    base.OnAppearing();
    if ((Application.Current.MainPage as MainMasterDetailPage) != null)
    {
        (Application.Current.MainPage as MainMasterDetailPage).IsGestureEnabled = false;
    }
}

protected override void OnDisappearing()
{
    base.OnDisappearing();
    if ((Application.Current.MainPage as MainMasterDetailPage) != null)
    {
        (Application.Current.MainPage as MainMasterDetailPage).IsGestureEnabled = true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM