我正在构建一个包含UITableview的应用。我的tableview工作得很完美,现在我想让细胞更符合我的需求。 tableviewcell包含标题,子标题和释放的图片。我觉得这很简单,但我不能让它起作用。我尝试从wrox Professional iPhone编程monotouch(页面:120和121)中遵循示例(和示例),但我不能让它在我的情况下工作。我也尝试过这个link和link,但无济于事。 第二个我做了一件事,这成了一个问题。
我的项目中有以下一些文件:
注意:我已经有了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部分类不能一起工作。再一次,这只是一种预感,我只是对出了什么问题一无所知。任何帮助表示赞赏。
答案 0 :(得分: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
上进行了跟踪主要问题是没有任何内容可以引用 GetCell 返回的托管'mycell',因此GC(垃圾收集器)可以(并且确实)收集/释放它。只有与单元格关联的所有内容都可以被收集,并且在您点击事件处理程序时会崩溃。
要解决此问题,请保留您创建的单元格的引用(例如List<>)。这将确保GC无法收集它们,并且其中的所有内容在需要时都将存在(例如事件处理程序)。
答案 1 :(得分:0)
它解决了!我认为这是某种错字。我孜孜不倦地走了一行代码,重复了Wrox书中的所有步骤,现在就可以了!所以我认为我在IB的某处写了一个错误的名字。
毕竟这不是积极的垃圾收集。但感谢你的建议!