【问题标题】:TableViewCell Content is disappearingTableViewCell 内容正在消失
【发布时间】:2014-03-29 23:35:06
【问题描述】:

我使用 Xamarin 创建了一个 Instagram 客户端类型的应用程序。当我最初运行应用程序时,它显示前 4 个或 5 个,当我一直向下滚动时,所有单元格都会消失并显示为空。以下是单元格的显示方式:

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 对象子类化到 TableView.cs 类下。很抱歉造成混乱:)

【问题讨论】:

  • 请添加更多信息,您提供的代码示例没有提供足够的信息。可以添加GetCell方法的代码吗?
  • @choper 我已经添加了获取单元格类
  • get cell上面的方法是bind方法@choper
  • “获取单元格是绑定方法”是什么意思?根本不是标准的 GetCell 方法,如果您使用的不是标准的 UITableViewSource 似乎没有覆盖然后通知这一点,因为我现在看到的不能与 UITableViewSource 一起使用
  • @choper 我提供了完整的课程和描述。很抱歉造成混乱。

标签: ios uitableview xamarin tableview instagram


【解决方案1】:

TableView 非常特别。它们重用在您的 ViewController(或您启动 TableViewSource 的任何位置)中定义的单元格。这意味着,如果您使用定义单元格的单个文件,并且有一个较长的 TableView,您需要向下滚动以查看 TableView 的其余部分,则 TableViewSource 将调用其基类中的某些方法。单元格的 Textfields 或 ImageViews 或其他任何内容内的数据不会被缓存。相反,大多数时候,数据将返回到其默认值,如可重复使用的单元格文件中定义的那样。有时,它会变得更加时髦,当您滚动时,您在顶部单元格之一中输入的数据会被复制到 TableView 下方的单元格中。一种选择是定义一个缓存,如列表,您可以将输入的数据放入 TextFields 中,并使用对 TableViews 主类的方法的覆盖来填充列表,最后放入将缓存中的值返回到正确的单元格中。

我对此也有很多麻烦,但请努力思考,最终你会得到它!

希望这会有所帮助。

祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多