• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 17:36:53 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我在 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap