2016-12-23 84 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