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

Use default implementation in my own "UITableViewController" to make methods optional

我在 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();
    }
}

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

我不知道它触发了什么,所以调用了方法。我可以实现 GetHeightForHeader 和 return UITableView.AutomaticDimension,但是我应该用 GetViewForHeader 做什么?

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

干脆不实施它们。我没有使用 Xamarin,但这些方法看起来来自 IUITableViewDataSourceIUITableViewDelegate - 这意味着它们在适当的时候由 table 视图调用。在 Cocoa 中,只有这两个协议中的两个方法被标记为必需,从您的示例中可以看出这两个:

public virtual System.nint RowsInSection(UIKit.UITableView tableview, System.nint section);
public virtual UIKit.UITableViewCell GetCell(UIKit.UITableView tableView, Foundation.NSIndexPath indexPath);

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

从这些docs来看,似乎Xamarin中的东西也很相似,因为只有提到的两个方法被标记为IsRequired=true

如果您坚持使用这些实现,那么您应该 return 尽可能使用中性值。在这种情况下,什么是 "neutral" 是另一回事,有待讨论。例如,header 高度可能是 0,其视图可能是 nil,但单元格高度可能是 44。至少这些是 Cocoa.

中的默认值