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.
Here's what I'm doing:
In Interface Builder:
- 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
" toTestCellView
and connected the UILabel to it. - Changed the identifier of the class to "TestCellView".
Implement 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;
}
}
}
** 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);
}
}
}
In my table's source:
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;
}
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.
What am I doing wrong?