srzzaz
srzzaz

Reputation: 21

xamarin ios collection view

Dears, i working in xamarin.ios that use collectionview my code is working fine i created custom cell view having imageview and label view My problem that : labelview didn't appear there are my code any one can help me

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

Upvotes: 1

Views: 1192

Answers (1)

PlusInfosys
PlusInfosys

Reputation: 3446

Implement GetSizeForItem method of UICollectionViewDelegateFlowLayout, to customise height of collectionviewcell according to content height.

    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;


    }

}

and in your view controller assign defined class as collection view delegate

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

Upvotes: 1

Related Questions