TableView.DeselectRow不触发自定义元素的取消选择事件

时间:2012-12-12 23:24:03

标签: uitableview xamarin.ios monotouch.dialog

我可能在这里遗漏了一些东西,但是,当我手动取消选择一行时,我无法在我的自定义Deselect类上触发Element事件。我重写单元格来做一些自定义绘图,我想在选择/取消选择单元格时更改字体的颜色。

下面详细介绍实际问题的实例。

public class TestViewController : DialogViewController
{
    public TestViewController () : base(UITableViewStyle.Plain, null, true)
    {
        Root = new RootElement(null);
        var section = new Section();
        for (int i = 0; i <= 10; i++)
        {
            var element = new MyCustomElement();
            element.Tapped += (dvc, tableView, indexPath) => {
                var sheet = new UIActionSheet("", null, "Cancel", null, null);
                sheet.Dismissed += delegate(object sender, UIButtonEventArgs e) {                   
                    tableView.DeselectRow(indexPath, false);
                };
                sheet.ShowInView(View);
            };
            section.Add (element);
        }
        Root.Add (section);
    }
}

public class MyCustomElement : Element, IElementSizing {
    static NSString mKey = new NSString ("MyCustomElement");

    public MyCustomElement () : base ("")
    {
    }

    public MyCustomElement (Action<DialogViewController,UITableView,NSIndexPath> tapped) : base ("")
    {
        Tapped += tapped;
    }

    public override UITableViewCell GetCell (UITableView tv)
    {
        var cell = tv.DequeueReusableCell (mKey);
        if (cell == null)
            cell = new UITableViewCell (UITableViewCellStyle.Default, mKey);
        return cell;
    }

    public float GetHeight (UITableView tableView, NSIndexPath indexPath)
    {
        return 65;
    }

    public event Action<DialogViewController, UITableView, NSIndexPath> Tapped;

    public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath path)
    {
        Console.WriteLine("Selected!");
        if (Tapped != null)
            Tapped (dvc, tableView, path);
    }

    public override void Deselected (DialogViewController dvc, UITableView tableView, NSIndexPath path)
    {
        // does not trigger when deselect manually invoked
        Console.WriteLine("Deselected!");
        base.Deselected (dvc, tableView, path);
    }
}

我还尝试覆盖Deselected本身的DialogViewController事件,甚至创建自定义Source并覆盖RowDeselected事件,但仍然没有触发。我触发它的唯一方法是删除Tapped处理程序并选择不同的单元格。

为了解决这个问题,我现在正在做的是手动强制元素在我调用DeselectRow后自行更新,但是,我想知道为什么它没有触发

1 个答案:

答案 0 :(得分:0)

您没有收到取消选择的事件,因为deselectRowAtIndexPath:animated:方法不会导致委托接收tableView:didDeselectRowAtIndexPath:消息,也不会向观察者发送UITableViewSelectionDidChangeNotification通知,并且调用此方法不会导致任何滚动到取消选择的行。

相关问题