2014-03-29 46 views
0

我创建了一个使用Xamarin的Instagram客户端类型应用程序。当我最初运行应用程序时,它会显示第一个4或5,当我一直向下滚动时,所有单元格消失并显示空白。下面是电池应该如何显示:TableViewCell内容正在消失

TableView.cs

public class TableView : UITableView, ITableCellProvider<Datum> 
    { 
     public TableView() 
     { 
     } 

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

     public UITableViewCell GetCell (Datum item) 
     { 
      var newCell = this.DequeueReusableCell(InstagramCell.Key) 
       as InstagramCell ?? InstagramCell.Create(); 

      newCell.Bind (item); 

      return newCell; 
     } 

     public float GetHeightForRow (NSIndexPath indexPath) 
     { 
      return 340f; 
     } 

    } 

InstagramCell.cs

public partial class InstagramCell : UITableViewCell 
{ 
    private Datum datum; 

    public static readonly UINib Nib = UINib.FromName ("InstagramCell", NSBundle.MainBundle); 
    public static readonly NSString Key = new NSString ("InstagramCell"); 

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

    public static InstagramCell Create() 
    { 
     return (InstagramCell)Nib.Instantiate (null, null) [0]; 
    } 

    public void Bind(Datum datum) 
    { 
     this.datum = datum; 
     if (this.datum == null || this.datum.caption == null) 
     { 
      this.TextLabel.Text = "null"; 
      return; 
     } 
     else 
     { 
      this.captionLabel.Text = datum.caption.text; 

     } 
     Task.Factory.StartNew (async() => { 
      this.pictureImage.InvokeOnMainThread (() => this.pictureImage.SetImage (
       url: new NSUrl (datum.images.standard_resolution.url) 
       ) 
      ); 

      this.profileImage.InvokeOnMainThread (() => this.profileImage.SetImage (
       url: new NSUrl (datum.user.profile_picture) 
       ) 
      ); 
     }); 
     this.nameLabel.Text = this.datum.user == null ? "user is null" : datum.user.full_name; 
    } 
} 

我已经使用SimplyMobile创建单元格内容。 ITableCellProvider是SimplyMobile的子类。我也在InstagramCell.cs中使用MonoTouch.dialogSystem.Threading.Tasks。在我的MainViewController.xib中,我继承TableView.cs类下的TableView对象。对不起,困惑:)

+0

请添加更多信息,您提供的代码示例没有提供足够的信息。你可以添加GetCell方法的代码吗? – choper

+0

@choper我已添加获取单元格类 – Prad

+0

获取单元格上方的方法是绑定方法@choper – Prad

回答

0

TableViews是非常特殊的。他们重复使用您的ViewController中定义的单元格(或者您启动TableViewSource的任何位置)。这意味着,如果您使用定义单元格的单个文件,并使用长TableView(需要向下滚动以查看TableView的其余部分),则TableViewSource将调用其基类中的某些方法。单元格Textfields或ImageViews内的数据或其他内容不会被缓存。相反,大多数情况下,数据将返回到默认值,如可重复使用的单元格文件中所定义的那样。有时候,它会变得更加怪异,并且您在顶部单元格中输入的数据会在滚动时复制到TableView中的单元格中。一个选项是定义一个缓存,就像一个List,在那里你可以把你输入的数据放在TextFields中,并且使用TableViews main类的方法的优先级来覆盖列表,最后put缓存中的值返回到正确的单元格中。

我对此也有很多麻烦,但努力想办法,最终得到它!

希望这会有所帮助。

祝你好运!