【发布时间】:2011-03-19 22:33:39
【问题描述】:
我希望能够在 IB 中设计自己的 UITableViewCell。 但是在尝试访问我在 IB 中定义的标签时,我不断收到 null ref 异常。
这就是我正在做的事情:
在界面生成器中:
- 我删除了“视图”并添加了一个 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 的解决方案。我在网上看到了几个这样做的例子。我只是认为这完全是矫枉过正。
我做错了什么?
【问题讨论】:
标签: xamarin.ios