2011-09-05 32 views
0

我正在構建一個包含UITableview的應用程序。我得到了tableview的完美工作,現在我想讓單元格更加具體到我的需求。 tableviewcell包含一個圖片的標題,子標題和釋放。我認爲這很容易做,但我無法得到它的工作。我嘗試從wrox Professional iPhone編程單點觸控(頁面:120和121)中學習示例(和示例),但我無法在我的情況下使用它。我試着按照這link和這link太,但無濟於事。 第二我做一件事情不同,它成爲一個問題。Monotouch - UITableview單元空引用異常

我有一些在我的項目的下列文件:

  • 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沒有被啓動(或引用)。感覺就像兩個RootViewController部分類不能一起工作。再次,這僅僅是一種預感,我對於發生了什麼問題一無所知。任何幫助表示讚賞。

回答

0

它解決了!我認爲這是某種錯字。我孜孜不倦地走過每一行代碼,重複Wrox書中的所有步驟,現在它就可以工作!所以我認爲我在IB的某個地方寫了一個錯誤的名字。

畢竟這不是積極的垃圾收集。但感謝您的建議!

+0

奇怪。有人警告說GC在模擬器上比在設備上運行更頻繁(通過設計來捕捉這些錯誤),因此在設備上運行時需要更多時間來「擊中」這種情況(但問題仍然存在)。 – poupou

1

這看起來像的Button in ContentView causes crash in MonoTouch runtime. Bug in Monotouch 4.0?

重複,也被跟蹤在Xamarin的Bugzilla @http://bugzilla.xamarin.com/show_bug.cgi?id=134

的主要問題是,沒有什麼參考管理「了myCell」由GetCell所以GC返回(垃圾收集器)可以(並確實)收集/釋放它。只有與單元格關聯的所有東西都可以收集起來,並且稍後會在您打開事件處理程序時崩潰。

要解決此問題,請保留您創建的單元格的參考(例如列表<>)。這將確保GC無法收集它們,並且在需要時它內的所有內容都將處於活動狀態(例如事件處理程序)。

+0

Thnx的答覆。我嘗試了你的建議,但仍然沒有成功。我不確定我是否正確。我做了一個mytableviewcell的全局列表,並且getCell我會引用特定的行。我仍然在單元格上的標籤上收到空引用異常。我如何知道它們已被實例化?有沒有適當的例子來說明如何做到這一點? – Adwen

+0

他們每次創建「mycell = new myTableViewCell();」被稱爲並且*可以*在從方法返回'mycell'值時被收集。 – poupou