簡體   English   中英

Xamarin 自定義 TableViewCell 中的出口為空

[英]Outlets in Xamarin custom TableViewCell are null

我正在嘗試使用自定義 UITableViewCell。 我能夠讓單元格實例化,因此它不為空。 但是,單元格中的所有 UILabel 插座都為空。 因此,我收到了空指針異常。

這類似於iOS 自定義 TableViewCell 類子視圖在填充 UITableViewController 時返回 null並且自定義單元格中的 Label 為 null 盡管如此,這兩種解決方案都沒有解決我的問題。 他們建議確保標識符匹配,它確實如此。 他們還告訴我注冊細胞以供重用,我也這樣做了。

我已確保單元格 Xib 文件中的標識符與我使用的標識符相匹配。

Xcode 中的 Table View Cell 顯示標識符

在我的 Source 類中:

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
    tableView.RegisterClassForCellReuse(typeof(BoardListCell), new NSString(BoardListCell.Key));
    var cell = (BoardListCell) tableView.DequeueReusableCell(BoardListCell.Key);
    if (cell == null) 
    {
        var nib = UINib.FromName(BoardListCell.Key, NSBundle.MainBundle);
        cell = (BoardListCell)nib.Instantiate (null, null) [0];
    }

    cell.setData("top", "not top"); //Sample data
    return cell;
}

在我的 BoardListCell 類中:

public partial class BoardListCell : UITableViewCell
{
    public static readonly UINib Nib = UINib.FromName ("BoardListCell", NSBundle.MainBundle);
    public static readonly NSString Key = new NSString ("BoardListCell");

    public BoardListCell() : base()
    {

    }

    public BoardListCell (IntPtr handle) : base (handle)
    {

    }

    public void setData(String top, String bottom)
    {
        exp.Text = top;
        playTime.Text = bottom;
    }
}

當我調試時,正在創建單元格。 然而,它的網點是空的。 如果我在構造函數中實例化它們,我不會收到錯誤消息,但這顯然違背了在 xcode 中制作原型的目的,因為它會清除布局等。

我遇到了上述相同的問題。 我的解決方案是刪除對TableView.RegisterClassForCellReuse(typeof(YourCustomeTableCell), "YourTableCellName");的調用TableView.RegisterClassForCellReuse(typeof(YourCustomeTableCell), "YourTableCellName");

如果您在 Xcode 設計器中創建了 Cell,則無需注冊 Cell。

希望這可以幫助任何仍然遇到此問題的人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM