在我自己的“UITableViewController”中使用默认实现来使方法可选

时间:2015-12-11 13:04:23

标签: c# ios inheritance xamarin xamarin.ios

我在Xamarin.iOS中编写了自己的UIViewController实现:

public class HUDTableViewController : UIViewController, IUITableViewDataSource, IUITableViewDelegate, IDisposable
{
    private UITableView tableView;

    public UITableView TableView
    {
        get
        {
            return this.tableView;
        }
        set
        {
            this.tableView = value;
        }
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        this.tableView = new UITableView();
        this.tableView.TranslatesAutoresizingMaskIntoConstraints = false;

        this.tableView.WeakDelegate = this;
        this.tableView.WeakDataSource = this;

        UIView parentView = new UIView();
        parentView.AddSubview(this.tableView);
        View = parentView;

        NSMutableDictionary viewsDictionary = new NSMutableDictionary();
        viewsDictionary["parent"] = parentView;
        viewsDictionary["tableView"] = this.tableView;

        parentView.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|[tableView]|", (NSLayoutFormatOptions)0, null, viewsDictionary));
        parentView.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:|[tableView]|", (NSLayoutFormatOptions)0, null, viewsDictionary));
    }

    [Foundation.Export("numberOfSectionsInTableView:")]
    public virtual System.nint NumberOfSections(UIKit.UITableView tableView)
    {
        throw new NotImplementedException();
    }

    public virtual System.nint RowsInSection(UIKit.UITableView tableview, System.nint section)
    {
        throw new NotImplementedException();
    }

    public virtual UIKit.UITableViewCell GetCell(UIKit.UITableView tableView, Foundation.NSIndexPath indexPath)
    {
        throw new NotImplementedException();
    }

    [Foundation.Export("tableView:didSelectRowAtIndexPath:")]
    public virtual void RowSelected(UIKit.UITableView tableView, Foundation.NSIndexPath indexPath)
    {
        throw new NotImplementedException();
    }

    [Foundation.Export("tableView:heightForHeaderInSection:")]
    public virtual System.nfloat GetHeightForHeader(UIKit.UITableView tableView, System.nint section)
    {
        throw new NotImplementedException();
    }

    [Foundation.Export("tableView:viewForHeaderInSection:")]
    public virtual UIKit.UIView GetViewForHeader(UIKit.UITableView tableView, System.nint section)
    {
        throw new NotImplementedException();
    }

    [Foundation.Export("tableView:willDisplayCell:forRowAtIndexPath:")]
    public virtual void WillDisplay(UIKit.UITableView tableView, UIKit.UITableViewCell cell, Foundation.NSIndexPath indexPath)
    {
        throw new NotImplementedException();
    }
}

现在我想使用该类并从中派生出来。其中一个派生需要GetHeightForHeaderGetViewForHeader,另一个则不需要。不需要它的那个似乎也调用这个函数,我得到NotImplementedException异常。

我不知道它触发了什么,因此调用了方法。我可以实施GetHeightForHeader并返回UITableView.AutomaticDimension,但我应该如何处理GetViewForHeader

如何让这个类足够灵活,以便所有派生都可以使用它并只调用他们需要的方法?我应该在方法中添加什么而不是throw new NotImplementedException();

1 个答案:

答案 0 :(得分:2)

根本不执行它们。我没有使用Xamarin,但这些方法看起来来自.pull-leftIUITableViewDataSource - 这意味着它们在适当的时候被表视图调用。在Cocoa中,只有这两个协议中的两个方法被标记为必需,从您的示例中可以看出这两个:

IUITableViewDelegate

其余部分完全是可选的,因此不需要默认实现,因为表格视图在未实现时已经知道如何处理它。

从这些docs来看,似乎Xamarin中的事情也非常相似,因为只有两个提到的方法被标记为public virtual System.nint RowsInSection(UIKit.UITableView tableview, System.nint section); public virtual UIKit.UITableViewCell GetCell(UIKit.UITableView tableView, Foundation.NSIndexPath indexPath);

如果您坚持使用这些实现,那么您应该尽可能返回中性值。在这种情况下,什么是“中立”是另一回事,需要辩论。标题高度可能为IsRequired=true,而其视图可能为0,而对于单元格高度则为nil。至少这些是Cocoa中的默认值。