iOS自定义TableViewCell类子视图返回null

我使用Xamarin iOSdevise器为我的TableViewdevise自定义TableViewCell类。 但是除了单元格本身,所有的单元格子视图属性(出口)都返回null。

我的自定义单元类:

partial class VehiclesTableCell : UITableViewCell { public VehiclesTableCell(IntPtr handle) : base(handle) { } public void UpdateCell(string licensePlate) { licensePlateLabel.Text = licensePlate; //hit the null reference exception for licensePlateLabel } } 

生成的部分类:

 [Register ("VehiclesTableCell")] partial class VehiclesTableCell { [Outlet] [GeneratedCode ("iOS Designer", "1.0")] UILabel licensePlateLabel { get; set; } void ReleaseDesignerOutlets () { if (licensePlateLabel != null) { licensePlateLabel.Dispose (); licensePlateLabel = null; } } } 

和我的Table Source类的GetCell:

 public class VehiclesTableSource : UITableViewSource { public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) { // Dequeueing the reuse cell var cell = (VehiclesTableCell)tableView.DequeueReusableCell(new NSString(typeof(VehiclesTableCell).Name)); // Set cell properties cell.UpdateCell(LicensePlate); // Return the cell with populated data return cell; } } 

正如我们所看到的,生成的代码有一个licensePlate的出口,为什么它的属性为空? 故事板不应该自动实例化所有子视图? 至less在所有其他情况下发生。

在这里输入图像说明

我有我的自定义TableViewCell相同的问题。 我发现问题是与TableView.RegisterClassForCellReuse (typeof(MyCell), MyCellId) 。 我不得不将其更改为TableView.RegisterNibForCellReuse(UINib.FromName("MyCell", NSBundle.MainBundle), MyCellId) ,以便加载我的.xib文件。

我自己遇到这个问题。 这是我做的解决它的方法:

  1. 在自定义单元的构造函数中实例化子视图的组件。 在你的情况下,像这样的东西:

    public VehiclesTableCell(IntPtr handle) : base(handle) { licensePlateLabel = new UILabel(); }

  2. 覆盖方法LayoutSubviews:

    public override void LayoutSubviews () { base.LayoutSubviews (); licensePlateLabel.Frame = new RectangleF(63, 5, 33, 33); // Your layout here }

本指南的信息让我了解了这一点,但理想情况下,有一种方法可以实现这一点,而无需手动实例化和布置子视图组件,因为它们已经在IOSdevise器中定义了。 希望有人可以用更好的方法来做到这一点…

编辑:我遇到了这个答案 ,这解释了为什么我无法绑定我在devise器中创build的自定义表格单元格。 TLDR:确保在检查器窗口中, Identity -> ClassTable View Cell -> Identifier都设置为自定义表格视图单元格类名称。

我有同样的问题,据我所知,如果你正在使用故事板,你可以在iOSdevise器的表格单元格的属性中input你的自定义类名称,它应该工作。 调用RegisterClassForCellReuse()是导致null子视图问题的原因。 一旦我删除该方法调用它似乎工作