Xamarin 在 iOS 中使用自定义 UITableViewCell 时抛出 NullReferenceException [英] Xamarin throws NullReferenceException when using custom UITableViewCell in iOS

查看:27
本文介绍了Xamarin 在 iOS 中使用自定义 UITableViewCell 时抛出 NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了与 Xamarin 自定义 UITableViewCell 抛出系统中讨论的问题非常相似的问题NullReferenceException.

尽管如此,按照那里的答案,我的问题还没有解决.

Nevertheless, after following the answer there, my issue has not been solved.

我有一个自定义的 UITableView BoardListCell.在我的故事板中,我有一个 UITableView.它可以在我的 ViewController 中通过出口 tableView 访问.

I have a custom UITableView BoardListCell. In my Storyboard, I have a UITableView. It is accessible in my ViewController via the outlet tableView.

在我的视图控制器中,我获得了数据.然后我做 tableView.Source = new BoardsTableViewSource(sourceData);

In my view controller, I get the data. Then I do 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 Exception.

When debugging, I get the NullReference Exception on the cell.setData() line.

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

In StoryBoard, I have my BoardListCell's identifier set to 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 的出口.我已经删除并重新添加了网点,但这并没有改变任何东西.

exp and playTime are outlets to UILabel's. I have deleted and re-added the outlets, but this hasn't changed anything.

我不知道是什么问题.我唯一能想到的是因为我使用的是我用 StoryBoard 制作的 UITableView 并通过插座访问它.尽管如此,我不知道问题是什么...

I don't know what the problem is. The only thing I can think of is because I am using a UITableView that I made with StoryBoard and am accessing it via an outlet. Nevertheless, I don't know what the problem is...

推荐答案

DequeueReusableCell 如果找不到合适的单元格来重用,它将返回一个空值.所以你需要在引用你的单元格之前明确检查:

DequeueReusableCell will return a null if it can't find a suitable cell to reuse. So you need to explicitly check for that before referencing your cell:

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

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

这篇关于Xamarin 在 iOS 中使用自定义 UITableViewCell 时抛出 NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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