[英]Why would EditActionsForRow never be called?
我絕對是iOS的初學者。
我想在表上提供向左滑動的動作,但是即使我設置了適當的委托子類,也永遠不會在其上調用EditActionsForRow方法。
這是我的代碼:
using System;
using UIKit;
using Foundation;
using System.Linq;
using System.Diagnostics;
namespace Tasky {
public partial class TaskyViewController : UIViewController {
TasksRepository tasksRepository;
TaskItemViewSource tasksViewSource;
public TaskyViewController (IntPtr handle) : base (handle) {
tasksRepository = new TasksRepository ();
tasksViewSource = new TaskItemViewSource ();
tasksViewSource.Data = new TaskItem[0];
tasksViewSource.DeleteClicked += HandleTaskDeleteClicked;
}
public override void ViewDidLoad () {
base.ViewDidLoad ();
TasksTable.Delegate = tasksViewSource;
TasksTable.Source = tasksViewSource;
}
public async override void ViewDidAppear (bool animated) {
base.ViewDidAppear (animated);
var tasks = await tasksRepository.GetAllTasksAsync ();
tasksViewSource.Data = tasks.ToArray ();
TasksTable.ReloadData ();
}
async void HandleTaskDeleteClicked (object sender, NSIndexPath e) {
await tasksRepository.DeleteTaskAsync (tasksViewSource.Data [e.Row]);
TasksTable.ReloadData ();
}
}
public class TaskItemViewSource : UITableViewSource, IUITableViewDelegate {
public TaskItem[] Data { get; set; }
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath) {
var task = Data [indexPath.Row];
UITableViewCellStyle cellStyle;
if (String.IsNullOrEmpty (task.Description)) {
cellStyle = UITableViewCellStyle.Default;
} else {
cellStyle = UITableViewCellStyle.Subtitle;
}
var taskCell = new UITableViewCell (cellStyle, "TaskCell");
taskCell.TextLabel.Text = Data [indexPath.Row].Title;
if (!String.IsNullOrEmpty (task.Description)) {
taskCell.DetailTextLabel.Text = Data [indexPath.Row].Description;
}
return taskCell;
}
public override nint RowsInSection (UITableView tableView, nint section) {
return Data.Length;
}
public override void RowSelected (UITableView tableView, NSIndexPath indexPath) {
Debug.WriteLine ("Deselected row #" + indexPath.Row);
tableView.DeselectRow (indexPath, true);
}
public event EventHandler<NSIndexPath> DeleteClicked;
public override UITableViewRowAction[] EditActionsForRow (UITableView tableView, NSIndexPath indexPath) {
Debug.WriteLine ("Inflating edit actions...");
var deleteButton = UITableViewRowAction.Create (
UITableViewRowActionStyle.Destructive,
"Delete",
(action, index) => {
if (DeleteClicked != null) {
DeleteClicked (this, index);
}
});
return new [] { deleteButton };
}
}
}
編輯:我設法與傑森的言論融合了委托和視圖源,取消選擇現在工作正常。
Xamarin的TableSource結合了兩個本地iOS概念,即DataSource和Delegate。 Xamarin還提供到DataSource和Delegate類的映射。 但是,您不應將TableSource與DataSource和Delegate混合使用。
從Xamarin的TableView文檔 :
如果您在查看Objective-C代碼或文檔,請注意Xamarin.iOS將UITableViewDelegate協議和UITableViewDataSource類聚合到這一類中。 在Objective-C中沒有可比的UITableViewSource類或協議。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.