如何使用 Interface Builder 中的自定义 UITableViewCell? [英] How to use custom UITableViewCell from Interface Builder?

查看:34
本文介绍了如何使用 Interface Builder 中的自定义 UITableViewCell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在 IB 中设计我自己的 UITableViewCell.但是在尝试访问我在 IB 中定义的标签时,我不断收到 null ref 异常.

I want to be able to design my own UITableViewCell in IB. But I keep getting a null ref exception when trying to access the label I defined in IB.

这是我正在做的事情:

在界面生成器中:

  • 我删除了视图"并添加了一个 UITableViewCell.
  • 将 UITableViewCell 的类更改为TestCellView".
  • 向单元格添加了 UILabel.
  • TestCellView 添加了一个出口oLblText"并将 UILabel 连接到它.
  • 将类的标识符更改为TestCellView".
  • I removed the "View" and added a UITableViewCell instead.
  • Changed the class of the UITableViewCell to "TestCellView".
  • Added a UILabel to the cell.
  • Added an outlet "oLblText" to TestCellView and connected the UILabel to it.
  • Changed the identifier of the class to "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**

** The 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 的解决方案.我在网上看到了几个这样做的例子.我只是觉得这完全是矫枉过正.

Please note that I do NOT want a solution that involves a UIViewController for every cell. I have seen a couple of examples on the web doing this. I just think it is total overkill.

我做错了什么?

推荐答案

首先,这是一个设计问题.如果您的表格视图始终加载具有静态内容的特定数量的单元格,例如在设置"视图中,请为您想要的每一行创建一个自定义单元格,并将每一行与一个插座连接起来.

First of all, it is a matter of design. If your table view always loads a specific number of cells with static content, like in a "Settings" view, create a custom cell for every row you want and connect each one with an outlet.

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

If this is not the case, then you have two options:

  1. 以编程方式创建一个继承 UITableViewCell 和每个视图的类,忘记界​​面生成器.
  2. 添加一个新的 iPhone 视图 带有控制器,替换其中的视图并像您一样对待它.除了您必须将手机与文件所有者中的插座连接这一事实之外.因此,当您实例化该控制器时,所有单元格的子视图都可以.
  1. Create a class that inherits the UITableViewCell and every view you want in it programmatically, forgetting Interface Builder.
  2. Add a new iPhone View with Controller, replace the view in there and treat it like you did. Except for the fact that you will have to connect your cell with an outlet in the File's Owner. So when you instantiate that controller, all your cell's subviews will be ok.

这不是矫枉过正,或者至少,Apple 推荐它:单击并转到从 Nib 文件加载自定义表格视图单元格"段落

It is not an overkill, or at least, Apple recommends it: click and go to the "Loading Custom Table-View Cells From Nib Files" paragraph

PS:刚刚遇到了类似的情况,我就是这样做的.在 MonoTouch 中,对于这个例子,你不需要 LoadNib 任何东西.只需在表源中的 GetCell 覆盖中执行此操作:

PS: Just had a similar situation and this is the way I've done it. In MonoTouch, for this example, you do not have to LoadNib anything. Just do this inside the GetCell override in your table's source:

using (CellController controller = new CellController())
{

     cell = (CustomCell)controller.View;

}

甚至可以在 CellController 对象内声明一个额外的出口,以避免从 UIView 投射.

Maybe even declare an extra outlet inside the CellController object to avoid casting from UIView.

这篇关于如何使用 Interface Builder 中的自定义 UITableViewCell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆