Monotouch - UITableview单元空引用异常 [英] Monotouch - UITableview Cell null reference exception

查看:100
本文介绍了Monotouch - UITableview单元空引用异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个包含UITableview的应用程序。我得到了tableview的完美工作,现在我想让单元格更加具体到我的需求。 tableviewcell包含一个图片的标题,子标题和释放。我认为这很容易做,但我无法得到它的工作。我尝试从wrox Professional iPhone编程单点触控(页面:120和121)中学习示例(和示例),但我无法在我的情况下使用它。我试着按照这个链接链接也是如此,但无济于事。
第二我做一件事情不同,它成为一个问题。



我的项目中有以下一些文件:




  • RootViewController(UITableViewController)
  • myTableViewCell(UITableViewCell)
  • BasicTableViewSource(UITableViewSource) li>


注意:我已经有一个RootViewController,但是当我为单元格创建接口时,我添加了一部分(用于RootViewController)以添加细胞进入。



以下是我的代码片段:

myTableViewCell.xib.designer.cs

  //基类型可能应该是MonoTouch.UIKit.UIViewController或子类
[MonoTouch.Foundation.Register(RootViewController)) ]
public partial class RootViewController {

private myTableViewCell __mt_Cell;

#pragma warning disable 0169
[MonoTouch.Foundation.Connect(Cell)]
private myTableViewCell Cell {
get {
this .__ mt_Cell =((myTableViewCell)(this.GetNativeField(Cell)));
返回这个.__ mt_Cell;
}
set {
this .__ mt_Cell = value;
this.SetNativeField(Cell,value);
}
}
}

//基类型可能应该是MonoTouch.UIKit.UITableViewCell或者子类
[MonoTouch.Foundation.Register(myTableViewCell )]
public partial class myTableViewCell {

private MonoTouch.UIKit.UIImageView __mt_img;

private MonoTouch.UIKit.UILabel __mt_lblInfo;

private MonoTouch.UIKit.UILabel __mt_lblReleaseDate;

私人MonoTouch.UIKit.UILabel __mt_lblTitle;

#pragma warning disable 0169
[MonoTouch.Foundation.Connect(img)]
private MonoTouch.UIKit.UIImageView img {
get {
this .__ mt_img =((MonoTouch.UIKit.UIImageView)(this.GetNativeField(img)));
返回这个.__ mt_img;
}
set {
this .__ mt_img = value;
this.SetNativeField(img,value);



[MonoTouch.Foundation.Connect(lblInfo)]
private MonoTouch.UIKit.UILabel lblInfo {
get {
this .__ mt_lblInfo =((MonoTouch.UIKit.UILabel)(this.GetNativeField(lblInfo)));
返回这个.__ mt_lblInfo;
}
set {
this .__ mt_lblInfo = value;
this.SetNativeField(lblInfo,value);



[MonoTouch.Foundation.Connect(lblReleaseDate)]
private MonoTouch.UIKit.UILabel lblReleaseDate {
get {
this .__ mt_lblReleaseDate =((MonoTouch.UIKit.UILabel)(this.GetNativeField(lblReleaseDate)));
返回这个.__ mt_lblReleaseDate;
}
set {
this .__ mt_lblReleaseDate = value;
this.SetNativeField(lblReleaseDate,value);



[MonoTouch.Foundation.Connect(lblTitle)]
private MonoTouch.UIKit.UILabel lblTitle {
get {
this .__ mt_lblTitle =((MonoTouch.UIKit.UILabel)(this.GetNativeField(lblTitle)));
返回这个.__ mt_lblTitle;
}
set {
this .__ mt_lblTitle = value;
this.SetNativeField(lblTitle,value);


$ / code $ / pre

BasicTableVIiewSource.cs

  public override UITableViewCell GetCell(UITableView tableView,MonoTouch.Foundation.NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell(this._cellIdentifier );

myTableViewCell mycell = null;
// if(cell == null)
// {
mycell = new myTableViewCell();
//NSBundle.MainBundle.LoadNib(\"RootViewController,_controller,null);
//_controller.myTableCell = new myTableViewCell();
// mycell = _controller.myTableCell;
// cell = new UITableViewCell(UITableViewCellStyle.Subtitle,this._cellIdentifier);
//}
// else
// {
// mycell =(myTableViewCell)cell;
//}


TrailerInfo item = this._tableItems [indexPath.Section] .items [indexPath.Row];

mycell.Title = item.Title;
mycell.Info =类型:+ item.genre;
mycell.ReleaseDate = Convert.ToDateTime(item.Releasedate).ToLongDateString();
mycell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
if(!string.IsNullOrEmpty(item.ImageURL))
{
mycell.myImage = item.image;

}

返回mycell;

}

RootViewController.xib.designer .cs

  //基类型可能应该是MonoTouch.UIKit.UIViewController或者子类
//[MonoTouch.Foundation .Register(RootViewController)]
public partial class RootViewController {

private MonoTouch.UIKit.UITableView __mt_view;

#pragma warning disable 0169
[MonoTouch.Foundation.Connect(view)]
private MonoTouch.UIKit.UITableView view {
get {
this .__ mt_view =((MonoTouch.UIKit.UITableView)(this.GetNativeField(view)));
返回这个.__ mt_view;
}
set {
this .__ mt_view = value;
this.SetNativeField(view,value);



$ / code $ / pre

RootViewController.xib

  public override void ViewDidAppear(bool some)
{
this.TableView.Source = new BasicTableViewSource(items,this );

$ / code>

正如你所看到的那样,我一直在改变代码。我不确定问题到底是什么。最近一直告诉我,属性(标题,信息,释放)为空。所以我假设mytableviewcell没有被启动(或引用)。感觉就像两个RootViewController部分类不能一起工作。再次,这仅仅是一种预感,我对于发生了什么问题一无所知。任何帮助表示感谢。

解决方案

它已经解决了!我认为这是某种错字。我孜孜不倦地走过每一行代码,重复Wrox书中的所有步骤,现在它就可以工作!所以我认为我在IB的某个地方写了一个错误的名字。

毕竟这不是积极的垃圾收集。但感谢您的建议!


I'm building an app that contains an UITableview. I got tableview to work perfectly now I want to make the cells more specific to my needs. The tableviewcell contains a picture a title, subititle and a releasedate. I thought this was simple to do but i cannot get it to work. I try to follow the example (and the sample) out of wrox Professional iPhone programming monotouch ( pages: 120 and 121) but I cannot get it to work in my case. I tried following this link and this link too, but to no avail. The second i do one thing differently it becomes a problem.

I have some of the following files in my project:

  • RootViewController (UITableViewController)
  • myTableViewCell (UITableViewCell)
  • BasicTableViewSource ( UITableViewSource)

Note: I already had a RootViewController but when I was creating the interface for the cell i've added a partial (for RootViewController) to add the cell into.

Here is my code snippets:

myTableViewCell.xib.designer.cs

// Base type probably should be MonoTouch.UIKit.UIViewController or subclass
[MonoTouch.Foundation.Register("RootViewController")]
public partial class RootViewController {

    private myTableViewCell __mt_Cell;

    #pragma warning disable 0169
    [MonoTouch.Foundation.Connect("Cell")]
    private myTableViewCell Cell {
        get {
            this.__mt_Cell = ((myTableViewCell)(this.GetNativeField("Cell")));
            return this.__mt_Cell;
        }
        set {
            this.__mt_Cell = value;
            this.SetNativeField("Cell", value);
        }
    }
}

// Base type probably should be MonoTouch.UIKit.UITableViewCell or subclass
[MonoTouch.Foundation.Register("myTableViewCell")]
public partial class myTableViewCell {

    private MonoTouch.UIKit.UIImageView __mt_img;

    private MonoTouch.UIKit.UILabel __mt_lblInfo;

    private MonoTouch.UIKit.UILabel __mt_lblReleaseDate;

    private MonoTouch.UIKit.UILabel __mt_lblTitle;

    #pragma warning disable 0169
    [MonoTouch.Foundation.Connect("img")]
    private MonoTouch.UIKit.UIImageView img {
        get {
            this.__mt_img = ((MonoTouch.UIKit.UIImageView)(this.GetNativeField("img")));
            return this.__mt_img;
        }
        set {
            this.__mt_img = value;
            this.SetNativeField("img", value);
        }
    }

    [MonoTouch.Foundation.Connect("lblInfo")]
    private MonoTouch.UIKit.UILabel lblInfo {
        get {
            this.__mt_lblInfo = ((MonoTouch.UIKit.UILabel)(this.GetNativeField("lblInfo")));
            return this.__mt_lblInfo;
        }
        set {
            this.__mt_lblInfo = value;
            this.SetNativeField("lblInfo", value);
        }
    }

    [MonoTouch.Foundation.Connect("lblReleaseDate")]
    private MonoTouch.UIKit.UILabel lblReleaseDate {
        get {
            this.__mt_lblReleaseDate = ((MonoTouch.UIKit.UILabel)(this.GetNativeField("lblReleaseDate")));
            return this.__mt_lblReleaseDate;
        }
        set {
            this.__mt_lblReleaseDate = value;
            this.SetNativeField("lblReleaseDate", value);
        }
    }

    [MonoTouch.Foundation.Connect("lblTitle")]
    private MonoTouch.UIKit.UILabel lblTitle {
        get {
            this.__mt_lblTitle = ((MonoTouch.UIKit.UILabel)(this.GetNativeField("lblTitle")));
            return this.__mt_lblTitle;
        }
        set {
            this.__mt_lblTitle = value;
            this.SetNativeField("lblTitle", value);
        }
    }

BasicTableVIiewSource.cs

        public override UITableViewCell GetCell(UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
    {
        UITableViewCell cell = tableView.DequeueReusableCell(this._cellIdentifier);

        myTableViewCell mycell = null;
        //if (cell == null)
        //{
            mycell = new myTableViewCell();
            //NSBundle.MainBundle.LoadNib("RootViewController", _controller, null);
            //_controller.myTableCell = new myTableViewCell();
            //mycell = _controller.myTableCell;
            //cell = new UITableViewCell(UITableViewCellStyle.Subtitle, this._cellIdentifier);
        //}
        //else
        //{
        //  mycell = (myTableViewCell)cell;
        //}


                TrailerInfo item = this._tableItems[indexPath.Section].items[indexPath.Row];

                mycell.Title = item.Title;
                mycell.Info = "Genre: " + item.genre ;  
                mycell.ReleaseDate = Convert.ToDateTime(item.Releasedate).ToLongDateString();
                mycell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
                if(!string.IsNullOrEmpty(item.ImageURL))
                {
                    mycell.myImage = item.image;

                }

    return mycell;

}

RootViewController.xib.designer.cs

// Base type probably should be MonoTouch.UIKit.UIViewController or subclass
//[MonoTouch.Foundation.Register("RootViewController")]
public partial class RootViewController {

    private MonoTouch.UIKit.UITableView __mt_view;

    #pragma warning disable 0169
    [MonoTouch.Foundation.Connect("view")]
    private MonoTouch.UIKit.UITableView view {
        get {
            this.__mt_view = ((MonoTouch.UIKit.UITableView)(this.GetNativeField("view")));
            return this.__mt_view;
        }
        set {
            this.__mt_view = value;
            this.SetNativeField("view", value);
        }
    }
}

RootViewController.xib

        public override void ViewDidAppear(bool some)
    {
        this.TableView.Source = new BasicTableViewSource(items, this);
    }

As you can see through time i've been changing the code. I'm not sure what exactly is the problem. Lately it keeps telling me that the properties (title, info, releasedate) are null. So i'm assuming that mytableviewcell is not being initiated (or referenced). It feels like the two RootViewControlller partial classes are not working together. Again this is just a hunch i'm simply clueless to what is going wrong. Any help is appreciated.

解决方案

It is solved! I think it was some sort of typo. I diligently walked every line of code repeating all the steps from the Wrox book and it works now! So I think that I wrote a name wrong somewhere in IB.

It wasn't aggressive garbage collection after all. but thanks for the suggestion!

这篇关于Monotouch - UITableview单元空引用异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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