ios - Monotouch - UITableview Cell 空引用异常

标签 ios uitableview mono garbage-collection xamarin.ios

我正在构建一个包含 UITableview 的应用程序。我让表格 View 可以完美工作,现在我想让单元格更具体地满足我的需求。 tableviewcell 包含图片、标题、副标题和发布日期。我认为这很简单,但我无法让它发挥作用。我尝试遵循 wrox Professional iPhone 编程单点触控(第 120 和 121 页)中的示例(和示例),但我无法让它在我的情况下工作。我尝试遵循此link还有这个link也一样,但是并没有什么卵用。 当我以不同的方式做一件事时,它就会成为一个问题。

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

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

注意:我已经有了一个 RootViewController,但是当我为单元格创建界面时,我添加了一个部分(用于 RootViewController)来将单元格添加到其中。

这是我的代码片段:

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);
    }

正如你所见,我一直在更改代码。我不确定到底是什么问题。最近它一直告诉我属性(标题、信息、发布日期)为空。所以我假设 mytableviewcell 没有被启动(或引用)。感觉两个 RootViewControlller 部分类不能一起工作。再说一遍,这只是一种预感,我根本不知道出了什么问题。如有任何帮助,我们将不胜感激。

最佳答案

这看起来像 Button in ContentView causes crash in MonoTouch runtime. Bug in Monotouch 4.0? 的重复项

并且还在 Xamarin 的 bugzilla @ http://bugzilla.xamarin.com/show_bug.cgi?id=134 上进行跟踪

主要问题是没有任何内容引用 GetCell 返回的托管“mycell”,因此 GC(垃圾收集器)可以(并且确实)收集/释放它。也可以收集仅与单元格关联的所有内容,稍后当您点击事件处理程序时,这些内容将会崩溃。

要解决此问题,请保留您创建的单元格的引用(例如列表<>)。这将确保 GC 无法收集它们,并且其中的所有内容在需要时都将处于事件状态(例如事件处理程序)。

关于ios - Monotouch - UITableview Cell 空引用异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7311295/

相关文章:

objective-c - 在顶点着色器中声明常量而不是文字。标准做法,还是不必要的严格?

c# - 从 Mono DLL 返回 List<Object>

objective-c - 在 iOS 中从 SQLite 保存和检索图像

ios - CompileXIB com.apple.InterfaceBuilder 错误 2001 操作无法完成

ios - Swift - collectionView 首次加载时如何自动选择单元格?

ios - cellForRow(在 : ) returns nil

ios - 显示不正确值的自定义 UITableViewCell

iphone - uitableview数据源过滤时didSelectRowAtIndexPath不起作用

mysql - 在 MacOS 的命令行上使用 nuget 安装包?

c# - 如何在 Gtk# 窗口上绘制一些东西(线、圆等)?