【问题标题】:xamarin ios collection viewxamarin ios 集合视图
【发布时间】:2016-12-26 19:56:12
【问题描述】:

亲爱的, 我在使用collectionview的xamarin.ios中工作我的代码工作正常我创建了具有图像视图和标签视图的自定义单元格视图 我的问题是:labelview没有出现 有我的代码任何人都可以帮助我

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

【问题讨论】:

    标签: c# xamarin.ios uicollectionview


    【解决方案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));
    

    【讨论】:

    • 没有名为 GetHeightForRow 的方法,我删除了 imageview 以仅显示标签,但标签没有出现
    • 更新了定义集合视图单元格大小的答案。顺便说一句,更改表格的颜色以检查其框架是否正在设置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 2021-07-07
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多