2016-12-23 133 views
1

逗人, 我在使用的CollectionView我的代碼工作xamarin.ios工作正常,我創建具有自定義單元格視圖的ImageView和標籤視圖 我的那個問題:LABELVIEW沒有出現 有我的任何代碼一個可以幫我xamarin IOS集合視圖

public class UserSource : UICollectionViewSource 
{ 
    public UserSource() 
    { 
     Rows = new List<UserElement>(); 
    } 

    public List<UserElement> Rows { get; private set; } 

    public Single FontSize { get; set; } 

    public SizeF ImageViewSize { get; set; } 


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

    public override nint GetItemsCount(UICollectionView collectionView, nint section) 
    { 
     return Rows.Count; 
    } 


    public override Boolean ShouldHighlightItem(UICollectionView collectionView, NSIndexPath indexPath) 
    { 
     return true; 
    } 

    public override void ItemHighlighted(UICollectionView collectionView, NSIndexPath indexPath) 
    { 
     var cell = (UserCell)collectionView.CellForItem(indexPath); 
     cell.ImageView.Alpha = 0.5f; 
    } 

    public override void ItemUnhighlighted(UICollectionView collectionView, NSIndexPath indexPath) 
    { 
     var cell = (UserCell)collectionView.CellForItem(indexPath); 
     cell.ImageView.Alpha = 1; 

     UserElement row = Rows[indexPath.Row]; 
     row.Tapped.Invoke(); 
    } 

    public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath) 
    { 
     var cell = (UserCell)collectionView.DequeueReusableCell(UserCell.CellID, indexPath); 

     UserElement row = Rows[indexPath.Row]; 
     cell.UpdateRow(row, FontSize, ImageViewSize); 

     return cell; 
    } 
} 

    public class UserCell : UICollectionViewCell 
{ 
    public static NSString CellID = new NSString("customCollectionCell"); 

    public UIImageView ImageView { get; private set; } 

    public UILabel LabelView { get; private set; } 
    [Export("initWithFrame:")] 
    public UserCell(RectangleF frame) 
     : base(frame) 
    { 
     ImageView = new UIImageView(); 
     ImageView.Layer.BorderColor = UIColor.DarkGray.CGColor; 
     ImageView.Layer.BorderWidth = 1f; 
     ImageView.Layer.CornerRadius = 3f; 
     ImageView.Layer.MasksToBounds = true; 
     // ImageView.ContentMode = UIViewContentMode.ScaleToFill; 
     // ContentView.AddSubview(ImageView); 

     LabelView = new UILabel(); 
     LabelView.BackgroundColor = UIColor.White; 
     LabelView.TextColor = UIColor.Black; 
     LabelView.TextAlignment = UITextAlignment.Center; 

     //ContentView.AddSubview(LabelView); 

     ContentView.AddSubviews(new UIView { LabelView, ImageView }); 

    } 



    public void UpdateRow(UserElement element, Single fontSize, SizeF imageViewSize) 
    { 
     LabelView.Text = element.Caption; 
     ImageView.Image = element.Image; 

     LabelView.Font = UIFont.FromName("HelveticaNeue-Bold", fontSize); 
     float bottom = float.Parse(ImageView.Frame.Bottom.ToString()); 
     float Width = float.Parse(imageViewSize.Width.ToString()); 
     float res = float.Parse((ContentView.Frame.Height - ImageView.Frame.Bottom).ToString()); 
     ImageView.Frame = new RectangleF(0, 0, imageViewSize.Width, imageViewSize.Height); 
     LabelView.Frame = new RectangleF(5, bottom, Width, res); 

    } 
} 

    public class UserElement 
{ 
    public UserElement(String caption, UIImage image, Action tapped) 
    { 
     Caption = caption; 
     Image = image; 
     Tapped = tapped; 
    } 

    public String Caption { get; set; } 

    public UIImage Image { get; set; } 

    public Action Tapped { get; set; } 
} 

回答

1

實施UICollectionViewDelegateFlowLayout的GetSizeForItem方法,根據內容的高度定製collectionviewcell的高度。

public class collectionViewFlowLayout : UICollectionViewDelegateFlowLayout 
{ 
    public CoreGraphics.CGSize cellsize; 
    public collectionViewFlowLayout(CoreGraphics.CGSize cSize) 
    { 
     this.cellsize = cSize; 
    } 
    public override CGSize GetSizeForItem(UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath) 
    { 
     return cellsize; 


    } 

}

,並在您的視圖控制器分配定義類作爲集合視圖代表

yourcollectionview.Delegate = new collectionViewFlowLayout(new CoreGraphics.CGSize(imgsize.Width, imgsize.Height+20)); 
+0

沒有叫GetHeightForRow方法和我刪除的ImageView僅顯示標籤,但標籤沒」 t出現 – srzzaz

+0

已更新的答案以定義集合視圖的單元大小。順便說一句,改變你的表的顏色來檢查其框架是否設置。 – Darshana