[英]Xamarin custom UITableViewCell throws System NullReferenceException
I'm creating a Xamarin app for iOS and I've added a UITableViewCell to a storyboard to give it my own style. 我正在为iOS创建一个Xamarin应用程序,我已经在故事板中添加了一个UITableViewCell,以赋予它我自己的风格。 I did add a class to this custom UITableViewCell, namely MainMenuCell.
我确实为这个自定义UITableViewCell添加了一个类,即MainMenuCell。 I added two labels to the cell and connected them with the MainMenuCell.h file, resulting in the following code:
我向单元格添加了两个标签,并将它们与MainMenuCell.h文件连接起来,产生以下代码:
MainMenuCell.cs MainMenuCell.cs
using System;
using Foundation;
using UIKit;
namespace MyProjectNamespace
{
public partial class MainMenuCell : UITableViewCell
{
public MainMenuCell (IntPtr handle) : base (handle)
{
}
public MainMenuCell () : base ()
{
}
public void SetCellData()
{
projectNameLabel.Text = "Project name";
projectDateLabel.Text = "Project date";
}
}
}
MainMenuCell.h (auto-generated): MainMenuCell.h(自动生成):
using Foundation;
using System.CodeDom.Compiler;
namespace MyProjectNamespace
{
[Register ("MainMenuCell")]
partial class MainMenuCell
{
[Outlet]
UIKit.UILabel projectDateLabel { get; set; }
[Outlet]
UIKit.UILabel projectNameLabel { get; set; }
void ReleaseDesignerOutlets ()
{
if (projectNameLabel != null) {
projectNameLabel.Dispose ();
projectNameLabel = null;
}
if (projectDateLabel != null) {
projectDateLabel.Dispose ();
projectDateLabel = null;
}
}
}
}
Now I have my UITableViewSource here, and I'm trying to initialize the MainMenuCell from the GetCell method: 现在我在这里有我的UITableViewSource,我正在尝试从GetCell方法初始化MainMenuCell:
using System;
using UIKit;
using Foundation;
namespace MyProjectNamespace
{
public class MainMenuSource : UITableViewSource
{
public MainMenuSource ()
{
}
public override nint NumberOfSections (UITableView tableView)
{
return 1;
}
public override string TitleForHeader (UITableView tableView, nint section)
{
return "Projects";
}
public override nint RowsInSection (UITableView tableview, nint section)
{
return 1;
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
MainMenuCell cell = new MainMenuCell();
cell.SetCellData ();
return cell;
}
}
}
However, it keeps throwing me a System.NullReferenceException at the line: 但是,它不断向我抛出System.NullReferenceException:
projectNameLabel.Text = "Project name";
It says: object reference not set to an instance of an object. 它说:对象引用未设置为对象的实例。
What am I missing here? 我在这里错过了什么? Any help would be highly appreciated.
任何帮助将受到高度赞赏。
You're almost there – instead of creating a new cell by yourself, let iOS do the work and dequeue the result. 你几乎就在那里 - 不是自己创建一个新单元,而是让iOS完成工作并将结果出列。
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var cell = (MainMenuCell)tableView.DequeueReusableCell("MainMenuCell");
cell.SetCellData();
return cell;
}
Note, that "MainMenuCell" is a identifier for the dynamic prototype cell from the Storyboard, you can name it whatever you want, but it has to be the same withing Storyboard and your Datasource. 请注意,“MainMenuCell”是Storyboard中动态原型单元的标识符,您可以根据需要为其命名,但它必须与Storyboard和您的数据源相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.