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

C# 在“我自己的”中使用默认实现;UITableViewController“;使方法可选,c#,ios,inheritance,xamarin,xamarin.ios,C#,Ios,Inheritance,Xamarin,Xamarin.ios,我已经在Xamarin.iOS中编写了自己的UIViewController实现: public class HUDTableViewController : UIViewController, IUITableViewDataSource, IUITableViewDelegate, IDisposable { private UITableView tableView; public UITableView TableView { get

我已经在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();
    }
}
现在我想使用这个类并从中派生。其中一个派生需要
GetHeightForHeader
GetViewForHeader
,另一个不需要。不需要它的那个似乎也调用了这个函数,我得到了
NotImplementedException
exception

我不知道它触发了什么,所以调用了这些方法。我可以实现
GetHeightForHeader
并返回
UITableView.AutomaticDimension
,但是如何使用
GetViewForHeader


如何使这个类足够灵活,以便所有派生都可以使用它,并且只调用它们需要的方法?我应该在方法中放入什么,而不是
抛出新的NotImplementedException()

只是不实现它们。我没有使用过Xamarin,但这些方法看起来来自
IUITableViewDataSource
IUITableViewDelegate
——这意味着它们在适当的时间被表视图调用。在Cocoa中,这两个协议中只有两个方法被标记为必需的,根据您的示例,这两个方法是:

public virtual System.nint RowsInSection(UIKit.UITableView tableview, System.nint section);
public virtual UIKit.UITableViewCell GetCell(UIKit.UITableView tableView, Foundation.NSIndexPath indexPath);
其余部分完全是可选的,因此不需要默认实现,因为表视图已经知道如何在未实现时处理它

从这些判断,似乎Xamarin中的情况也非常相似,因为只有提到的两种方法被标记为
IsRequired=true


如果坚持使用这些实现,那么应该尽可能返回中性值。在这种情况下,什么是“中立”是另一回事,有待讨论。例如,标题高度可能为
0
,视图可能为
nil
,而单元格高度可能为
44
。至少这些是Cocoa中的默认值。

感谢您的回复。是的,只需要这两种方法。如果删除虚拟方法,我会得到NumberOfSections(UIKit.UITableView)标记为重写,但找不到合适的方法进行重写(CS0115)。如果我删除
覆盖
,我将消除此错误。例如,我在派生类中有
public nint NumberOfSections(UITableView tableView)
。这是可以编译的,但是我的头完全丢失了,一些部分丢失了,等等。因此,派生类中的方法不再被调用。我还需要补充什么?您必须实现适当的方法。如果您想要一个节标题,那么可以使用
GetHeightForHeader
GetViewForHeader
。如果缺少节,默认值为
1
。如果你想有更多的节,你应该实现
NumberOfSections
,并在那里返回一个正确的值。你看过吗?不久前;-)我所记得的只是关于使用
UITableViewController
的一般情况,而不是这里的特殊情况。但我可以再看一眼。关于您的其他消息:实现了这些方法(没有
覆盖
),但没有调用它们。因为节数的默认值是
1
,所以我只能看到第一节。调用
节时,实际上对您来说并不重要,这些是实现细节,在一般情况下并不重要。你只需要关心你是否需要它们,如果需要,从它们那里返回什么。例如,
titleForHeader
有一个,因此如果您想要定制一些东西,应该使用and