【发布时间】:2020-03-14 12:53:30
【问题描述】:
当我试图通过向左滑动来删除表格视图中的一行时,滑动不起作用。有时在 10-12 次尝试后,表格视图行滑动,然后我可以看到删除按钮。下面是我正在使用的代码。请让我知道我哪里做错了?
注意:问题出现在 iPhone 上。在 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)
);
}
}
}
}
}
【问题讨论】:
-
你的项目是 Xamarin.forms 项目还是 Xamarin.iOS 项目?
-
你的代码看起来不错。您的 PlaylistViewCell 中是否有任何滑动手势?
-
@JackHua-MSFT :实际上 Master Detail Page 滑动手势已启用。禁用它有效。有什么方法可以仅在此页面上禁用 Master Detail Page Swipe 手势??
-
在页面的OnAppearing方法中禁用它并在OnDisappearing方法中启用它。
-
好的。谢谢@JackHua-MSFT
标签: c# xamarin xamarin.forms xamarin.ios prism