简体   繁体   English

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

[英]iOS custom TableViewCell class subviews return null

I use Xamarin iOS designer to design custom TableViewCell class for my TableView. 我使用Xamarin iOS设计器为我的TableView设计自定义TableViewCell类。 But all cell subviews properties (outlets) return null except cell itself. 但是除了单元本身之外,所有单元子视图属性(出口)都返回null。

My custom cell class: 我的自定义单元类:

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 
    }
}

Generated partial class: 生成的部分类:

[Register ("VehiclesTableCell")]
partial class VehiclesTableCell
{
    [Outlet]
    [GeneratedCode ("iOS Designer", "1.0")]
    UILabel licensePlateLabel { get; set; }

    void ReleaseDesignerOutlets ()
    {
        if (licensePlateLabel != null) {
            licensePlateLabel.Dispose ();
            licensePlateLabel = null;
        }
    }
}

And GetCell of my Table Source class: 和我的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;
    }
}

As we see genereted code has an outlet for licensePlate so why is its property null? 我们看到genereted代码有licensePlate的出口,那么为什么它的属性为null? Is storyboard not supposed to instantiate all of its subviews automatically? 故事板是不是应该自动实例化其所有子视图? At least its happening in all other situations. 至少它发生在所有其他情况。

在此输入图像描述

I was having the same problem with my custom TableViewCell . 我的自定义TableViewCell遇到了同样的问题。 I found out the issue was with TableView.RegisterClassForCellReuse (typeof(MyCell), MyCellId) . 我发现问题出在TableView.RegisterClassForCellReuse (typeof(MyCell), MyCellId) I had to change it to TableView.RegisterNibForCellReuse(UINib.FromName("MyCell", NSBundle.MainBundle), MyCellId) in order to get my .xib loaded. 我不得不将其更改为TableView.RegisterNibForCellReuse(UINib.FromName("MyCell", NSBundle.MainBundle), MyCellId)以便加载我的.xib。

I came across this issue myself. 我自己遇到过这个问题。 This is what I did to resolve it: 这就是我解决它的方法:

  1. Instantiate the components of the subview in the constructor of the custom cell. 在自定义单元格的构造函数中实例化子视图的组件。 In your case, something like: 在你的情况下,像:

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

  2. Override the method LayoutSubviews: 覆盖方法LayoutSubviews:

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

The info on this guide got me this far, but ideally there would be a way to accomplish this without instantiating and laying out the subviews components manually as they're already defined in the IOS designer. 本指南中的信息让我走得很远,但理想情况下,如果没有在IOS设计器中定义子视图组件的情况下手动实例化和布局子组件,则可以实现此目的。 Hopefully someone can chime in with a better way to do this... 希望有人可以用更好的方式来实现这个目标......

EDIT: I came across this answer which explained why I wasn't able to bind the custom table cell I created in the designer. 编辑:我遇到了这个答案 ,解释了为什么我无法绑定我在设计器中创建的自定义表格单元格。 TLDR: Ensure that Identity -> Class and Table View Cell -> Identifier are both set to the custom table view cell class name in the inspector window. TLDR:确保Identity -> ClassTable View Cell -> Identifier都设置为检查器窗口中的自定义表视图单元类名称。

I had this same issue, and as far as I have found out, if you are using storyboard you can just enter your custom class name in the properties of your table cell in the iOS designer and it should work. 我有同样的问题,据我所知,如果你使用的是故事板,你可以在iOS设计器的表格单元属性中输入你的自定义类名,它应该可以工作。 Calling RegisterClassForCellReuse() is what causes the null sub views issue. 调用RegisterClassForCellReuse()是导致null子视图问题的原因。 Once I removed that method call it seemed to work 一旦我删除了该方法调用它似乎工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM