为什么EditActionsForRow永远不会被调用?

时间:2015-03-08 15:54:16

标签: ios xamarin

我是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 };
        }
    }
}

修改 我设法将代表和视图源与Jason的评论融合在一起,取消选择现在工作正常。

1 个答案:

答案 0 :(得分:0)

Xamarin的TableSource结合了两个本机iOS概念,即DataSource和Delegate。 Xamarin还提供了DataSource和Delegate类的映射。但是,您不应将TableSource与DataSource和Delegate混合使用。

来自Xamarin的TableView docs

  

如果您正在查看Objective-C代码或文档,请注意这一点   Xamarin.iOS聚合UITableViewDelegate协议和   UITableViewDataSource类进入这一类。没有   Objective-C中可比较的UITableViewSource类或协议。