填充 UITableViewController 时自定义单元格中的标签为空 [英] Label in custom cell is null when populating UITableViewController

查看:25
本文介绍了填充 UITableViewController 时自定义单元格中的标签为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Xamarin.iOS 构建 iOS 应用.我正在使用故事板并使用自定义单元格创建了一个带有分组表的 UITableViewController.该单元格包含一个标签,我要为其分配一个值.

I'm using Xamarin.iOS to build an iOS app. I'm using a storyboard and have created a UITableViewController with a grouped table using custom cells. The cell contains a label to which I want to assign a value.

覆盖数据源中的 getcell 方法来设置标签的 text 属性会抛出空异常,我找不到原因?我检查了插座,它在那里.有关如何查找错误的任何提示?

Overriding the getcell method in the datasource to set the text property of the label throws a null exception and I can't find why? I've checked the outlet and it's there. Any tips on how to find the error?

public partial class NameListScreen : UITableViewController
{
    MyTableSource _source = null;

    public override void ViewWillAppear (bool animated)
    {
        base.ViewWillAppear (animated);

        LoadNameList();          
    }

    void LoadNameList ()
    {
        var service = new MyService();
        var names = service.GetAllNames();

        _source = new MyTableSource(names);

        this.TableView.Source = _source;
    }
}

public class MyTableSource : UITableViewSource {

    protected List<string> _names;
    protected string _cellIdentifier = "MyCell";

    public MyTableSource (List<string> items)
    {
        _names = items;
    }

    public override int RowsInSection (UITableView tableview, int section)
    { 
        return _names.Count;
    }

    public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
    {
        // Request a recycled cell to save memory
        var cell = (MyTableCell) tableView.DequeueReusableCell(_cellIdentifier);

        // If there are no cells to reuse, create a new one
        if (cell == null)
        cell = new MyTableCell();

        cell.UpdateCell(indexPath, _names[indexPath.Item]);

        return cell;
    }
}


public partial class MyTableCell : UITableViewCell
{

    public MyTableCell () : base ()
    { }

    public MyTableCell (IntPtr handle) : base (handle)
    { }

    public void UpdateCell(NSIndexPath indexPath, string name)
    {
        this._indexPath = indexPath;
        this._id = track.TrackId;
        this.NameLabel.Text = name;   //This row throws exception as NameLabel is null, why? 
    }
}

推荐答案

根据您的问题,我认为您没有以正确的方式加载 xib.在您的自定义单元格中放置一个如下所示的静态方法.

Based on your question I think you did not load the xib in the correct manner. Inside you custom cell just put a static method like the following.

// Inside MyTableCell
public static MyTableCell Create()
{
    NSArray topLevelObjects = NSBundle.MainBundle.LoadNib("MyTableCell", null, null);
    MyTableCell cell = Runtime.GetNSObject(topLevelObjects.ValueAt(0)) as MyTableCell;
    return cell;
}

此模式现在用于新的 Xamarin 单元创建模板.唯一的区别是使用了 UINib 类(我无法验证它,因为我没有随身携带 Xamarin Studio).

This pattern is now used in new Xamarin cell creation template. The only difference is that UINib class is used (I can't verify it since I don't have Xamarin Studio with me).

然后,在 GetCell 方法中,您应该执行以下操作

Then, in GetCell method you should do like the following

public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
    // Request a recycled cell to save memory
    var cell = (MyTableCell) tableView.DequeueReusableCell(_cellIdentifier);

    // If there are no cells to reuse, create a new one
    if (cell == null)
        cell = MyTableCell.Create();

    cell.UpdateCell(indexPath, _names[indexPath.Item]);
    return cell;
}

如果你还没有注册,你需要在.cs[Register("MyTableCell")]才能在XIB中使用.您还需要在 XIB 文件中将设计单元的类型设置为 MyTableCell.

You need to [Register("MyTableCell")] in .cs for using in XIB if you haven't registered it yet. You also need to set the type of the designed cell as MyTableCell in your XIB file.

但是,稍微使用 Xamarin 模板(如果您创建新的 iOS 文件,您可以选择它们),您会发现一个自定义单元格模板.

But, play a bit with Xamarin template (you can choose them if you create a new iOS file) and you will find a custom cell template.

这篇关于填充 UITableViewController 时自定义单元格中的标签为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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