gpt4 book ai didi

ios - Xamarin 在 iOS 中使用自定义 UITableViewCell 时抛出 NullReferenceException

转载 作者:行者123 更新时间:2023-11-28 19:39:40 24 4
gpt4 key购买 nike

我遇到的问题与 Xamarin custom UITableViewCell throws System NullReferenceException 中讨论的问题非常相似.

然而,按照那里的答案,我的问题并没有得到解决。

我有一个自定义的 UITableView BoardListCell。在我的 Storyboard 中,我有一个 UITableView。它可以在我的 ViewController 中通过导出 tableView 访问。

在我的 View Controller 中,我获取了数据。然后我执行 tableView.Source = new BoardsTableViewSource(sourceData);

我的BoardsTableViewSource:

using System;
using System.Collections.Generic;
using Foundation;
using UIKit;

public class BoardsTableViewSource : UITableViewSource
{
private List<List<Object>> data;
Boolean normalBoards;

public BoardsTableViewSource (List<List<Object>> data, bool normalBoards)
{
this.data = data;
this.normalBoards = normalBoards;
}

public List<List<Object>> getData()
{
return data;
}

public override nint RowsInSection (UITableView tableview, nint section)
{
return data.Count;
}

public override string TitleForHeader (UITableView tableView, nint section)
{
return "Header";
}

public override string TitleForFooter (UITableView tableView, nint section)
{
return "Footer";
}

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
BoardListCell cell = (BoardListCell)tableView.DequeueReusableCell("BoardTableCell");

List<Object> cellData = data [indexPath.Row];

cell.setData("1","!"); //I have simplified the data for this example

}
}
}

调试时,我在 cell.setData() 行收到 NullReference 异常。

在 StoryBoard 中,我将 BoardListCell 的标识符设置为 BoardTableCell

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 static BoardListCell Create ()
{
return (BoardListCell)Nib.Instantiate (null, null) [0];
}

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

expplayTimeUILabel 的导出。我已经删除并重新添加了网点,但这并没有改变任何东西。

不知道是什么问题。我唯一能想到的是因为我正在使用我用 StoryBoard 制作的 UITableView 并通过 socket 访问它。不过,我不知道问题是什么......

最佳答案

如果 DequeueReusableCell 找不到合适的单元格进行重用,它将返回 null。所以你需要在引用你的单元格之前明确地检查它:

BoardListCell cell = (BoardListCell)tableView.DequeueReusableCell("BoardTableCell");

if (cell == null) {
cell = new BoardListCell();
}

关于ios - Xamarin 在 iOS 中使用自定义 UITableViewCell 时抛出 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35261905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com