iOS自定义TableViewCell类 subview 返回null

标签 ios uitableview xamarin.ios xamarin

我使用 Xamarin iOS 设计器为我的 TableView 设计自定义 TableViewCell 类。 但是除单元格本身外,所有单元格 subview 属性( socket )都返回 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;
        }
    }
}

还有我的表源类的 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 导出,那么为什么它的属性为 null? Storyboard不应该自动实例化它的所有 subview 吗? 至少它发生在所有其他情况下。

enter image description here

最佳答案

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

关于iOS自定义TableViewCell类 subview 返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24485823/

相关文章:

ios - 为 NSData iOS 中的每一位设置数据

ios - 如何在 UIView 表格单元格中制作幻灯片

ios - Xamarin 无法在 iPhone 模拟器上运行 iOS 应用程序 - 不支持 386 架构

visual-studio - 尝试为 iOS 手机构建跨平台 Xamarin 应用程序时出现 "Failed to resolve Plugin.Permissions.Abstractions.Permission"错误

ios - Mach-o 链接错误

iphone - NSUndoManager 和 GLKit

iOS 将 View 底部的约束添加到容器中心

ios - UITextView exclusionPaths 右下角

ios - 在代码中设置 UITableViewCell 子类

c# - Xamarin Forms 在 Webview 导航问题中处理超链接