【问题标题】:iOS custom TableViewCell class subviews return nulliOS 自定义 TableViewCell 类子视图返回 null
【发布时间】:2014-06-30 08:25:02
【问题描述】:

我使用 Xamarin iOS 设计器为我的 TableView 设计自定义 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 的出口,那么为什么它的属性为空? 故事板不应该自动实例化它的所有子视图吗? 至少在所有其他情况下都会发生这种情况。

【问题讨论】:

  • 如果在 GetCell 方法中为空,您应该创建单元格。 Check this 他们展示了如何在单元格为空时创建单元格。
  • Cell 不为空,只有它的属性 Labels、ImageViews 等
  • 只是想一想,检查 IBOutlets 连接,因为我们没有使用 Xamarin 工作室,因此无法了解 Check this too
  • @JanakNirmal,通过在控制器中为 TableView 注册的自定义单元格类,可以在 UITableViewSource 的 GetCell 方法中将单元格出列,而无需额外的 null 检查。 TableView.RegisterClassForCellReuse (typeof(MyCell), MyCellId);
  • @Tomzie,是的,我愿意。只需在 ViewDidLoad 方法中删除行:TableView.RegisterClassForCellReuse(typeof(VehiclesTableCell), new NSString("VehicleCell")); 即可消除不必要的自定义单元注册。

标签: ios uitableview xamarin.ios xamarin


【解决方案1】:

我的自定义 TableViewCell 遇到了同样的问题。我发现问题出在TableView.RegisterClassForCellReuse (typeof(MyCell), MyCellId)。我必须将其更改为 TableView.RegisterNibForCellReuse(UINib.FromName("MyCell", NSBundle.MainBundle), MyCellId) 才能加载我的 .xib。

【讨论】:

    【解决方案2】:

    我自己也遇到过这个问题。这就是我为解决它所做的:

    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 }

    this guide 上的信息让我走到了这一步,但理想情况下,有一种方法可以实现这一点,而无需手动实例化和布置子视图组件,因为它们已经在 IOS 设计器中定义。希望有人可以提出更好的方法来做到这一点......

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

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,据我所知,如果您使用的是故事板,您只需在 iOS 设计器的表格单元格的属性中输入您的自定义类名称,它应该可以工作。调用RegisterClassForCellReuse() 是导致null 子视图问题的原因。一旦我删除了该方法调用,它似乎就可以工作了

      【讨论】:

      • 谢谢!如果您使用 StoryBoard,这就是答案!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-19
      • 1970-01-01
      相关资源
      最近更新 更多