iOS自定义TableViewCell类子视图返回null [英] iOS custom TableViewCell class subviews return null

查看:91
本文介绍了iOS自定义TableViewCell类子视图返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use Xamarin iOS designer to design custom TableViewCell class for my TableView. But all cell subviews properties (outlets) return null except cell itself.

我的自定义单元类:

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

生成的部分class:

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

我表源的GetCell class:

And GetCell of my Table Source class:

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的插座所以为什么它的属性为null?
故事板是不是应该自动实例化其所有子视图?
至少在所有其他情况下发生。

As we see genereted code has an outlet for licensePlate so why is its property null? Is storyboard not supposed to instantiate all of its subviews automatically? At least its happening in all other situations.

推荐答案

我的自定义 TableViewCell <我遇到了同样的问题/ code>。我发现问题出在 TableView.RegisterClassForCellReuse(typeof(MyCell),MyCellId)。我不得不将其更改为 TableView.RegisterNibForCellReuse(UINib.FromName(MyCell,NSBundle.MainBundle),MyCellId)以便加载我的.xib。

I was having the same problem with my custom TableViewCell. I found out the issue was with 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.

这篇关于iOS自定义TableViewCell类子视图返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆