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

标签 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();

最佳答案

干脆不要实现它们。我没有使用 Xamarin,但这些方法看起来来自 IUITableViewDataSourceIUITableViewDelegate - 这意味着它们在适当的时候由 TableView 调用。在 Cocoa 中,只有这两个协议(protocol)中的两个方法被标记为必需,从你的例子来看就是这两个:

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

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

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

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

关于c# - 在我自己的 "UITableViewController"中使用默认实现使方法可选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34224264/

相关文章:

c# - TransactionScope 如何回滚事务?

c# - RestSharp 可以在不使用多部分内容类型的情况下发送二进制数据吗?

ios - iOS 上的 2D 游戏

c++ - 在多级继承中从中间类调用派生类函数而不将其标记为虚拟

c# - 检查GroupBy Key是否存在

c# - VisualStudio 错误 : An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System. Data.dll

iOS 流在中间切割长消息

ios - 此 bundle 无效 info.plist 中所需的 key 设备

c++ - 基类构造函数的段错误

C#继承防止基类方法调用派生类方法