OGeek|极客世界-中国程序员成长平台

标题: c# - 在我自己的 "UITableViewController"中使用默认实现使方法可选 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 17:36
标题: c# - 在我自己的 "UITableViewController"中使用默认实现使方法可选

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



Best Answer-推荐答案


根本不执行它们。我没有使用 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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4