1

我有UICollectionView的問題,與單元格完全一樣......當我向下滾動時,所以某些單元格只是空的。他們只是消失是這樣的:Xamarin UICollectionView一些單元格在向下滾動和向後滾動後消失

enter image description here

我viewDidLoad中()在OrdersView:

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; 
} 
+0

你能解決你的問題嗎? –

+0

我會在星期一嘗試解決這個問題...但老實說,我很困惑這個解決方案在Swift代碼中... – pnk

+0

請嘗試我的建議,設置cell.label.text =「你的文本」,如我的答案。我知道代碼很快,但在這種情況下你不需要代碼。鏈接答案的解釋顯示了單元格內容消失的原因。 –

回答

0

我有一個非常類似的問題。這是因爲你的表重用了單元格。 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

相關問題