ios - UICollectionView 重复和乱序显示项目

标签 ios xamarin xamarin.ios uicollectionview custom-renderer

我已经为 UICollectionView 构建了一个自定义渲染器。

我一直遇到一个问题,我想不出可能的解决办法,就在这里。

每当用户滚动 UICollectionView 时,下一个要显示在屏幕上的项目就会乱序重复显示。

您可以在 GitHub 中找到我的代码:https://github.com/DanielCauser/XamarinHorizontalList

这个 gif 显示出了什么问题,一些列表项出现不止一次而且顺序不对。

我认为这是由于竞争条件,操作系统只是将数据加载到它在该帧可用的任何视单元中。

enter image description here

这是我在 Xamarin.Forms 中的 View :

<local:HorizontalViewNative ItemsSource="{Binding Monkeys}"
                            Grid.Row="5"
                            VerticalOptions="Start"
                            ItemHeight="100"
                            ItemWidth="100">
                <local:HorizontalViewNative.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <ContentView>
                                <StackLayout WidthRequest="100"
                                             HeightRequest="100">
                                    <Image Source="{Binding Image}" />

                                    <Label Text="{Binding Name}"
                                               LineBreakMode="MiddleTruncation"
                                               HorizontalTextAlignment="Center"
                                               VerticalTextAlignment="Center"/>
                                </StackLayout>
                            </ContentView>
                        </ViewCell>
                    </DataTemplate>
                </local:HorizontalViewNative.ItemTemplate>
            </local:HorizontalViewNative>

这是我在 Xamarin.Forms 项目中的自定义控件:

public class HorizontalViewNative : View
    {
        public static readonly BindableProperty ItemsSourceProperty =
            BindableProperty.Create("ItemsSource", typeof(IEnumerable), typeof(HorizontalViewNative), default(IEnumerable<object>), BindingMode.TwoWay, propertyChanged: ItemsSourceChanged);

        public static readonly BindableProperty ItemTemplateProperty =
            BindableProperty.Create("ItemTemplate", typeof(DataTemplate), typeof(HVScrollGridView), default(DataTemplate));

        public static readonly BindableProperty ItemHeightProperty =
            BindableProperty.Create("ItemHeight", typeof(int), typeof(HVScrollGridView), default(int));

        public static readonly BindableProperty ItemWidthProperty =
            BindableProperty.Create("ItemWidth", typeof(int), typeof(HVScrollGridView), default(int));

        public IEnumerable ItemsSource
        {
            get { return (IEnumerable)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }

        public DataTemplate ItemTemplate
        {
            get { return (DataTemplate)GetValue(ItemTemplateProperty); }
            set { SetValue(ItemTemplateProperty, value); }
        }

        public int ItemHeight
        {
            get { return (int)GetValue(ItemHeightProperty); }
            set { SetValue(ItemHeightProperty, value); }
        }

        public int ItemWidth
        {
            get { return (int)GetValue(ItemWidthProperty); }
            set { SetValue(ItemWidthProperty, value); }
        }

        private static void ItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var itemsLayout = (HorizontalViewNative)bindable;
        }
    }

这是我在 iOS 项目中的自定义渲染(使用 UICollectionView、UICollectionViewSource 和 UICollectionViewCell)。

[assembly: ExportRenderer(typeof(HorizontalViewNative), typeof(iOSHorizontalViewRenderer))]
namespace XamarinHorizontalList.iOS
{
    public class iOSHorizontalViewRenderer : ViewRenderer<HorizontalViewNative, UICollectionView>
    {
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(HorizontalViewNative.ItemsSource))
            {
                Control.Source = new iOSViewSource(Element as HorizontalViewNative);
                Control.RegisterClassForCell(typeof(iOSViewCell), nameof(iOSViewCell));
            }
        }

        protected override void OnElementChanged(ElementChangedEventArgs<HorizontalViewNative> e)
        {
            base.OnElementChanged(e);

            if (Control == null)
            {
                var layout = new UICollectionViewFlowLayout();
                layout.ScrollDirection = UICollectionViewScrollDirection.Horizontal;

                if (e.NewElement != null)
                {
                    layout.ItemSize = new CGSize(e.NewElement.ItemWidth, e.NewElement.ItemHeight);
                    layout.MinimumInteritemSpacing = 0;
                    layout.MinimumLineSpacing = 0;

                    var rect = new CGRect(0, 0, 100, 100);
                    SetNativeControl(new UICollectionView(rect, layout));
                    Control.BackgroundColor = e.NewElement?.BackgroundColor.ToUIColor();
                }
            }
        }
    }

    public class iOSViewSource : UICollectionViewSource
    {
        private readonly HorizontalViewNative _view;

        private readonly IList _dataSource;

        public iOSViewSource(HorizontalViewNative view)
        {
            _view = view;
            _dataSource = view.ItemsSource?.Cast<object>()?.ToList();
        }

        public override nint NumberOfSections(UICollectionView collectionView)
        {
            return 1;
        }

        public override nint GetItemsCount(UICollectionView collectionView, nint section)
        {
            return _dataSource != null ? _dataSource.Count : 0;
        }

        public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
        {
            iOSViewCell cell = (iOSViewCell)collectionView.DequeueReusableCell(nameof(iOSViewCell), indexPath);
            var dataContext = _dataSource[indexPath.Row];
            Debug.WriteLine(((Monkey)dataContext).Name);
            if (dataContext != null)
            {
                var dataTemplate = _view.ItemTemplate;
                ViewCell viewCell;
                var selector = dataTemplate as DataTemplateSelector;
                if (selector != null)
                {
                    var template = selector.SelectTemplate(_dataSource[indexPath.Row], _view.Parent);
                    viewCell = template.CreateContent() as ViewCell;
                }
                else
                {
                    viewCell = dataTemplate?.CreateContent() as ViewCell;
                }

                cell.UpdateUi(viewCell, dataContext, _view);
            }
            return cell;
        }
    }

    public class iOSViewCell : UICollectionViewCell
    {
        private UIView _view;

        public iOSViewCell(IntPtr p) : base(p)
        {
        }

        public void UpdateUi(ViewCell viewCell, object dataContext, HorizontalViewNative view)
        {
            viewCell.BindingContext = dataContext;
            viewCell.Parent = view;

            var height = (int)((view.ItemHeight + viewCell.View.Margin.Top + viewCell.View.Margin.Bottom));
            var width = (int)((view.ItemWidth + viewCell.View.Margin.Left + viewCell.View.Margin.Right));
            viewCell.View.Layout(new Rectangle(0, 0, width, height));

            if (Platform.GetRenderer(viewCell.View) == null)
            {
                Platform.SetRenderer(viewCell.View, Platform.CreateRenderer(viewCell.View));
            }
            var renderer = Platform.GetRenderer(viewCell.View).NativeView;

            if (_view == null)
            {
                renderer.ContentMode = UIViewContentMode.ScaleAspectFit;
                ContentView.AddSubview(renderer);
            }
            _view = renderer;
        }
    }
}

最佳答案

在 iOS 上,UICollectionView 将重用单元格,因为您调用了 DequeueReusableCell(nameof(iOSViewCell), indexPath);

这意味着当 Collection View 首次加载其内容时,它要求其数据源为每个可见项提供一个 View 。为了简化代码的创建过程, Collection View 要求您始终使 View 出队,而不是在代码中显式创建它们。参见 here了解更多详情。

因此,您的 UICollectionView 一开始似乎可以很好地处理最初的四个单元格。但是在你滚动它之后单元格的内容变得困惑,因为这个声明是不正确:

if (_view == null)
{
    renderer.ContentMode = UIViewContentMode.ScaleAspectFit;
    ContentView.AddSubview(renderer);
}

UICollectionView 尝试重用队列中的 Cell 时,属性 _view 不会为 null,因此 ContentView 不会添加新的 subview 。然后您的单元格会显示旧图像和文本。你可以试试这个来修复它:

//if (_view == null)
//{
//Remove all subViews from contentView when the cell being reused.
foreach(UIView subView in ContentView.Subviews)
{
    subView.RemoveFromSuperview();
}
renderer.ContentMode = UIViewContentMode.ScaleAspectFit;
ContentView.AddSubview(renderer);
//}
//_view = renderer;

但我想推荐你做的是:既然你尝试使用渲染器,为什么不尝试使用本地方法来创建你的单元格、数据源?

首先,像这样修改你的单元格结构:

UIImageView imgView;
UILabel label;
public iOSViewCell(IntPtr p) : base(p)
{
    imgView = new UIImageView(new CGRect(0, 0, 100, 80));
    ContentView.AddSubview(imgView);
    imgView.ContentMode = UIViewContentMode.ScaleAspectFit;

    label = new UILabel(new CGRect(0, 80, 100, 20));
    label.TextAlignment = UITextAlignment.Center;
    label.LineBreakMode = UILineBreakMode.MiddleTruncation;
    ContentView.AddSubview(label);
}

同时更改 UpdateUi() 方法:

public void UpdateUi(object dataContext)
{
    Monkey monkey = dataContext as Monkey;
    imgView.Image = UIImage.FromBundle(monkey.Image);
    label.Text = monkey.Name;
}

最后一步是修改 GetCell() 事件:

public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
    iOSViewCell cell = (iOSViewCell)collectionView.DequeueReusableCell(nameof(iOSViewCell), indexPath);
    var dataContext = _dataSource[indexPath.Row];
    if (dataContext != null)
    {
        cell.UpdateUi(dataContext);
    }
    return cell;
}

这样scrollView可以更流畅的滚动。

关于ios - UICollectionView 重复和乱序显示项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48630149/

相关文章:

ios - 升级到 MonoTouch 6 后自动旋转停止工作

ios - iTunes 联盟计划 : Confirm if purchase is successful

ios - 前导点的自动完成在 swift 中不起作用

ios - 如何在 Swift 中动态更改包含 UICollectionView 单元格的 UITableView 单元格的高度?

xamarin - 正在运行的应用程序中,程序包未正确签名(NO_CERTIFICATES)

ios - 在 iOS 中向接受客户端证书的 Web 服务器发出 Web 请求

iphone - 使用 MPVolumeView 后如何重新打开系统音量覆盖?

Android WindowSoftInputMode.AdjustResize 仍在平移

ios - 如何在 Xamarin ios 中获取导航栏后退按钮的大小?

android - MonoTouch + MonoDroid : Can project files live in same folder?