【问题标题】:Xamarin UICollectionView some cells disappear after scrolling down and backXamarin UICollectionView 一些单元格在向下和向后滚动后消失
【发布时间】:2017-04-06 10:52:25
【问题描述】:

我遇到了 UICollectionView 的问题,正是单元格...当我向下和向后滚动时,有些单元格只是空的。他们只是像这样消失:

我在 OrdersView 中的 ViewDidLoad():

public override void ViewDidLoad()
      {
            Title = "My Orders..";

            base.ViewDidLoad();

            ordersCollectionView.Frame = new CGRect(ordersCollectionView.Frame.X, ordersCollectionView.Frame.Y, ordersCollectionView.Frame.Width, ordersCollectionView.Frame.Height);
            ordersCollectionView.ScrollEnabled = true;

            var layout = new UICollectionViewFlowLayout
            {
                ItemSize = new CGSize() { Width = ordersCollectionView.Bounds.Width, Height = 80 },
                MinimumInteritemSpacing = 0,
                MinimumLineSpacing = 2,
                ScrollDirection = UICollectionViewScrollDirection.Vertical
            };

            ordersCollectionView.SetCollectionViewLayout(layout, true);

            this.ClearBindings(_source);
            _source = new OrdersCollectionViewSource(ordersCollectionView);

            this.AddBindings(new Dictionary<object, string>
                {
                    { _source, "ItemsSource Orders; SelectedItem SelectedOrder; SelectionChangedCommand ShowOrderDetailCommand" }
                });

            ordersCollectionView.Source = _source;
            ordersCollectionView.ReloadData();

            Mvx.Resolve<IMvxMessenger>().SubscribeOnMainThread<OrdersLoadedMessage>(mess =>
            {
                this.ClearBindings(_source);

                _source = new OrdersCollectionViewSource(ordersCollectionView);
                this.AddBindings(new Dictionary<object, string>
                        {
                            { _source, "ItemsSource Orders; SelectedItem SelectedOrder; SelectionChangedCommand ShowOrderDetailCommand" }
                        });

                ordersCollectionView.Source = _source;
                ordersCollectionView.ReloadData();

            }, MvxReference.Strong);

            InitializeRefreshControl();

            ViewMessages.ViewCreatedMessages(this, "OrdersView", ViewModel);
        }

OrdersCollectionViewSource:

 public class OrdersCollectionViewSource : MvxCollectionViewSource
    {
        private static readonly NSString CellIdentifier = new NSString("OrdersCollectionViewCell");

        public OrdersCollectionViewSource(UICollectionView collectionView) : base(collectionView)
        {
            collectionView.RegisterClassForCell(typeof(OrdersCollectionViewCell), CellIdentifier);
        }

        protected override UICollectionViewCell GetOrCreateCellFor(UICollectionView collectionView, NSIndexPath indexPath, object item)
        {
            var cell = (UICollectionViewCell)collectionView.DequeueReusableCell(CellIdentifier, indexPath);
            cell.BackgroundColor = UIColor.FromRGB(237, 237, 237);
            return cell;
        }
    }

OrdersCollectionViewCell:

   public class OrdersCollectionViewCell : MvxCollectionViewCell
    {
        [Export("initWithFrame:")]
        public OrdersCollectionViewCell(CGRect frame) : base(frame)
        {
            Layer.BackgroundColor = new CGColor(220, 25, 25, 255);

            //left
            var labelName = new UILabel();
            labelName.Frame = new CGRect(10f, 15f, (float)(Bounds.Width / 2 + 20), 20f); 
            labelName.Font = UIFont.PreferredHeadline;
            labelName.TextColor = UIColor.FromRGBA(90, 24, 24, 255);
            labelName.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin;
            labelName.Text = "Text";

            var labelPickupDate = new UILabel();
            labelPickupDate.Frame = new CGRect(10f, 45f, (float)(Bounds.Width / 2 - 10), 20f);
            labelPickupDate.Font = UIFont.PreferredBody;
            labelPickupDate.TextColor = UIColor.Black;
            labelPickupDate.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin;

            //right
            var labelPrice = new UILabel();
            labelPrice.Frame = new CGRect((float)(Bounds.Width / 2), 45f, (float)(Bounds.Width / 2 - 10), 20f);
            labelPrice.TextAlignment = UITextAlignment.Right;
            labelPrice.Font = UIFont.PreferredBody;
            labelPrice.TextColor = UIColor.Black;
            labelPrice.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin;

            var labelStatus = new UILabel();
            labelStatus.Frame = new CGRect((float)(Bounds.Width / 2), 15f, (float)(Bounds.Width / 2 - 10f), 20f);
            labelStatus.TextAlignment = UITextAlignment.Right;
            labelStatus.Font = UIFont.PreferredHeadline;
            labelStatus.TextColor = UIColor.FromRGBA(15, 113, 10, 255);
            labelStatus.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin;

            Add(labelName);
            Add(labelPickupDate);
            Add(labelStatus);
            Add(labelPrice);

            this.DelayBind(() =>
            {
                var set = this.CreateBindingSet<OrdersCollectionViewCell, Order>();
                set.Bind(labelPickupDate).For(q => q.Text).To(vm => vm.PickupDate).WithConversion("StringDateTimeConverter");
                set.Bind(labelStatus).For(q => q.Text).To(vm => vm.OrderStatus);
                set.Bind(labelPrice).To(vm => vm.Sum).WithConversion("CreditConverter");

                set.Apply();
            });
        }
    }

我在这里发现了一些类似的问题,但一切都在 xcode 中。所以我尝试了一些方法,但它仍然无法正常工作。

编辑 - 我的解决方案:

protected override UICollectionViewCell GetOrCreateCellFor(UICollectionView collectionView, NSIndexPath indexPath, object item)
{
      var cell = (UICollectionViewCell)collectionView.DequeueReusableCell(CellIdentifier, indexPath);
      cell.BackgroundColor = UIColor.FromRGB(237, 237, 237);

      var order = (Order) item;

      var test = (OrdersCollectionViewCell) cell;
      test.labelName.Text = "Name";
      test.labelPickupDate.Text = StringConvertor.StringDateTime(order.PickupDate);
      test.labelPrice.Text = StringConvertor.PriceConvertor(order.Sum);
      test.labelStatus.Text = order.OrderStatus;

      return test;
}

【问题讨论】:

  • 你能解决你的问题吗?
  • 我会在星期一尝试解决这个问题......但老实说,我对 Swift 代码中的这个解决方案有点困惑......
  • 请尝试我的建议,按照我的回答设置 cell.label.text = "your text"。我知道代码很快,但在这种情况下你不需要代码。链接答案的解释显示了单元格内容消失的原因。
  • 嗯我仍然不知道如何在 swift 代码中使用你的解决方案来我在 c# 中的实现......但也许我有另一个解决方案......我做了 collectionView.ScrollEnabled = false;并将collectionView Height 设置为项目数* 单元格高度,然后我将collectionView 放入ScrollView 中......现在它正在工作......你对这个解决方案有什么看法?
  • 所以单元格的数量适合滚动视图而不需要滚动,对吗?这将解决您的错误。但是,如果您需要更多单元格并且需要滚动怎么办?如果这样做,您仍然需要修复您在原始问题中提到的错误。目前,您在构造函数中为:OrdersCollectionViewCell 设置了文本。我建议的解决方案只是在 GetOrCreateCellFor 函数中再次设置您的文本变量。

标签: ios xamarin xamarin.ios uicollectionview uicollectionviewcell


【解决方案1】:

我有一个非常相似的问题。这是因为您的表格重用了单元格。 dequeueReusableCellWithIdentifier 将在滚动时重复使用相同的单元格引用,并且可能不会保留您要显示的文本。

您的GetOrCreateCellFor 函数包含对DequeueReusableCell 的调用。您还应该在此函数中设置单元格的文本(例如cell.labelName.text = "my text here")。

protected override UICollectionViewCell GetOrCreateCellFor(UICollectionView collectionView, NSIndexPath indexPath, object item)
{
    var cell = (UICollectionViewCell)collectionView.DequeueReusableCell(CellIdentifier, indexPath);
    cell.BackgroundColor = UIColor.FromRGB(237, 237, 237);
    cell.labelName.text = "Your label here"
    cell.labelPickupDate.text = "Your pickup date here"
    cell.labelPrice.text = "Your price here"
    cell.labelStatus.text = "Your status here"
    return cell;
}

请参阅下面的答案以获取完整说明。如果您有任何问题,请告诉我。

https://stackoverflow.com/a/43164656/2179970

【讨论】:

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