HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了xamarin.ios – 如何使用Interface Builder中的自定义UITableViewCell?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我希望能够在IB中设计我自己的UITableViewCell.
但是当我尝试访问我在IB中定义的标签时,我一直得到一个空引用异常.

这是我正在做的事情:

在Interface Builder中:

>我删除了“视图”并添加一个UITableViewCell.
>将uITableViewCell的类更改为“TESTCellView”.
>在单元格中添加了UILabel.
>向TESTCellView添加一个“oLblText”插座,并将uILabel连接到它.
>将类的标识符更改为“TESTCellView”.

实现TESTCellView.xib.cs

public partial class TESTCellView : UITableViewCell
{
    public TESTCellView(String sKey) : base(UITableViewCellStyle.Default,sKey)
    {
    }

    public TESTCellView(IntPtr oHandlE) : base(oHandlE)
    {
    }

    public String TestText
    {
        get
        {
            return this.oLblText.Text;
        }
        set
        {
                        // HERE I get the null ref exception!
            this.oLblText.Text = value;
        }
    }
}

** TESTCellView.designer.cs **

[MonoTouch.Foundation.Register("TESTCellView")]
public partial class TESTCellView {

    private MonoTouch.UIKit.UILabel __mt_oLblText;

    #pragma warning disable 0169
    [MonoTouch.Foundation.Connect("oLblText")]
    private MonoTouch.UIKit.UILabel oLblText {
        get {
            this.__mt_oLblText = ((MonoTouch.UIKit.UILabel)(this.GetNativeField("oLblText")));
            return this.__mt_oLblText;
        }
        set {
            this.__mt_oLblText = value;
            this.SetNativeField("oLblText",value);
        }
    }
}

在我的表的来源:

public override UITableViewCell GetCell (UITableView tableView,NSIndexPath indexPath)
{
    TESTCellView oCell = (TESTCellView)tableView.DequeueReusableCell("myCell");
    if(oCell == null)
    {
        // I suppose this is wrong but how to do it correctly?
                // this == my UITableViewsource.
                NSBundle.MainBundle.LoadNib("TESTCellView",this,null);
        oCell = new TESTCellView("myCell");
    }
    oCell.TestText = "Cell " + indexPath.Row;
    return oCell;
}

请注意,我不想要一个涉及每个单元格的UIViewController的解决方案.我在网上看到了几个这样做的例子.我只是认为这是完全矫枉过正的.

我究竟做错了什么?

@H_489_37@解决方法
首先,这是一个设计问题.如果表视图始终加载具有静态内容的特定数量的单元格,例如在“设置”视图中,请为所需的每一行创建自定义单元格,并将每个单元格与插座连接.

如果不是这种情况,那么您有两种选择:

>创建一个以编程方式继承UITableViewCell和您想要的每个视图的类,忘记Interface Builder.
>使用COntroller添加新的iPhone视图,替换其中的视图并像对待一样对待它.除了@R_801_10245@文件连接到文件所有者中的插座这一事实.因此,当您实例化该控制器时,所有单元格的子视图都可以.

这不是一种矫枉过正,或者至少Apple推荐它:click and go to the “Loading Custom Table-View Cells From Nib Files” paragraph

PS:刚刚有类似的情况,这就是我做的方式.在MonoTouch中,对于此示例,您不必对LoadNib进行任何操作.只需在表的源代码中的GetCell覆盖内执行此操作:

using (CellController controller = new CellController())
{

     cell = (Customcatell)controller.View;

}

甚至可能在CellController对象中声明一个额外的插座,以避免从UIView转换.

大佬总结

以上是大佬教程为你收集整理的xamarin.ios – 如何使用Interface Builder中的自定义UITableViewCell?全部内容,希望文章能够帮你解决xamarin.ios – 如何使用Interface Builder中的自定义UITableViewCell?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。