简体   繁体   中英

Why would EditActionsForRow never be called?

I'm an absolute beginner at iOS.

I want to provide swipe-left actions on a table but even though i am setting a proper delegate subclass the EditActionsForRow method is never called on it.

Here's my code:

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

EDIT: I managed to fuse the delegate and the view source with Jason's remark, deselecting is now working fine.

Xamarin's TableSource combines two native iOS concepts, the DataSource and the Delegate. Xamarin also provides mappings to the DataSource and Delegate classes. However, you should not mix TableSource with DataSource and Delegate.

From Xamarin's TableView docs :

If you are looking at Objective-C code or documentation, be aware that Xamarin.iOS aggregates the UITableViewDelegate protocol and UITableViewDataSource class into this one class. There is no comparable UITableViewSource class or protocol in Objective-C.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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