简体   繁体   English

滚动时出现MvxTableViewSource DequeueReusableCell问题

[英]MvxTableViewSource DequeueReusableCell issue when scrolling

We have set-up our tableviewSource and a tableview but are having issues with the data appearing in the wrong cells and select states being applied to multiple cell when scrolling a large amount of cells. 我们已经设置了tableviewSource和tableview,但是在出现错误的单元格时出现数据问题,并且在滚动大量单元格时选择应用于多个单元格的状态。

The table cells are set up according to n+1 tutorial on table cells/table views and only cause problems when part of the table is hidden due to size. 表格单元格是根据表格单元格/表格视图中的n + 1教程设置的,只有在因大小而隐藏部分表格时才会导致问题。

Any ideas on how to solve this? 关于如何解决这个问题的任何想法?

var menuItem = item as MenuItemViewModel;

if (menuItem != null && menuItem.ViewModelType == null)
{
    NSString cellId = new NSString("Menu Header Cell");
    var headerCell = tableView.DequeueReusableCell(cellId, indexPath) as MenuItemHeaderCell;

    if (headerCell == null)
    {
        headerCell = new UITableViewCell(UITableViewCellStyle.Default, cellId) as MenuItemHeaderCell;
    }
    return headerCell;
}
else
{
    NSString cellId = new NSString("Menu Cell");
    var cell = tableView.DequeueReusableCell(cellId, indexPath) as MenuItemCell;
    if (cell == null)
    {
        cell = new UITableViewCell(UITableViewCellStyle.Default, cellId) as MenuItemCell;
    }
    return cell;
}

Following your comment I have updated the code as follows: 根据您的评论,我已更新代码如下:

private static readonly NSString MenuHeaderCellIdentifier = new NSString("Menu Header Cell"); private static readonly NSString MenuHeaderCellIdentifier = new NSString(“Menu Header Cell”); private static readonly NSString MenuItemCellIdentifier = new NSString("Menu Cell"); private static readonly NSString MenuItemCellIdentifier = new NSString(“Menu Cell”);

        public MenuTableViewSource(UITableView tableView): base(tableView)
        {
            tableView.RegisterClassForCellReuse(typeof(MenuItemHeaderCell), MenuHeaderCellIdentifier);
            tableView.RegisterClassForCellReuse(typeof(MenuItemCell), MenuItemCellIdentifier);
        }


        protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
        {
            NSString cellIdentifier;

            var menuItem = item as MenuItemViewModel;
            if (menuItem.ViewModelType == null)
            {
                cellIdentifier = MenuHeaderCellIdentifier;
            }
            else
            {
                cellIdentifier = MenuItemCellIdentifier;

            }
            var cell = (UITableViewCell)TableView.DequeueReusableCell(cellIdentifier, indexPath);
            return cell;

        }

And the bindings are set up as follows: 绑定设置如下:

var source = new MenuTableViewSource(TableView);
TableView.Source = source;
TableView.RowHeight = 46;

var set = this.CreateBindingSet<LeftMenuView, LeftMenuViewModel>();
set.Bind(source).To(vm => vm.MenuItems);
set.Apply();

TableView.ReloadData();

And the re-use issue is still happening. 而重用问题仍在发生。 Basically if cells are off screen they start to pick up incorrect values when scrolling. 基本上,如果单元格在屏幕外,则在滚动时会开始拾取不正确的值。 Is there anything else we should be doing? 还有什么我们应该做的吗?

Thanks Stuart for your sample code. 感谢Stuart提供的示例代码。 After many hours of investigating the issue I found it was only happening on iOS7 after a few more hours I realised it was down to the fact we adding a locally created variable and then delay binding to it, which seemed to result in the value going out of scope. 经过几个小时的调查问题后,我发现它只在iOS7上发生了几个小时之后,我意识到这是因为我们添加了一个本地创建的变量,然后延迟绑定到它,这似乎导致价值消失范围。 Changing this to a member variable stopped this happening, what a long day, added to the bank of gotcha's. 将此更改为成员变量可以阻止这种情况发生,这是漫长的一天,添加到了陷阱的银行。

I spent 10 minutes creating this sample app - github.com/slodge/ListApp 我花了10分钟创建这个示例应用程序 - github.com/slodge/ListApp

It uses DequeueReusableCell code exactly like the code shown in your question and it seems to work fine 它使用DequeueReusableCell代码,就像您的问题中显示的代码一样,它似乎工作正常

例

Given this result, I suspect the problem needs investigating within your app. 鉴于此结果,我怀疑问题需要在您的应用中进行调查。

I would suggest: 我会建议:

  1. making a cup of tea, 泡一杯茶
  2. looking at the display carefully - can you identify which cells and which cell values are "incorrect"? 仔细观察显示器 - 你能识别哪些细胞和哪些细胞值“不正确”? can you add a debug field to those cells which tells you whether the cell has the correct list item as it's datacontext 你可以向那些单元格添加一个调试字段,告诉你单元格是否具有正确的列表项,因为它是datacontext
  3. once you have identified what is going wrong, look at how those "incorrect" cells and cell values are created and set - this may lead you to identify what the problem is 一旦确定出现了什么问题,请查看如何创建和设置这些“不正确”的单元格和单元格值 - 这可能会导致您确定问题所在
  4. if not, then try to reproduce the effects in a separate test app like https://github.com/slodge/ListApp - this will be something you can share with your colleagues and with others on here in order that they can assist 如果没有,那么尝试在像https://github.com/slodge/ListApp这样的单独测试应用程序中重现效果 - 这将是您可以与您的同事和其他人共享的内容,以便他们可以协助

As far as I can see, this is just normal debugging and development (especially step 1) 据我所知,这只是正常的调试和开发(特别是第1步)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM