UICollectionView单元格不显示(MonoTouch / Xamarin)

时间:2014-05-08 15:07:11

标签: ios xamarin.ios xamarin uicollectionview

在Xamarin中使用UICollectionView就像我以前一样,但遇到了一个奇怪的问题 - 当包含集合的视图加载时,我看到了一小段细胞,然后它们消失了。我仔细检查了CollectionView的ContentSize,显然它默认为0宽度,0高度,但手动设置它似乎无法解决问题。 CollectionView似乎坚持下去(如果我将背景颜色设置为黑色,我看到父母中的黑色视图)但细胞正在消失

父视图(UIView子类):

UICollectionViewFlowLayout layout = new UICollectionViewFlowLayout ();
layout.ItemSize = new SizeF (274, 281);
layout.MinimumInteritemSpacing = 3;
layout.ScrollDirection = UICollectionViewScrollDirection.Horizontal;

var haulCollection = new HaulCollectionController(layout);
haulCollection.CollectionView.Frame = new RectangleF (0, cellHeader.Frame.Bottom,    cellHeader.Frame.Width, 281);
AddSubview(haulCollection.CollectionView);

UICollectionViewController:

public class HaulCollectionController : UICollectionViewController
{

    public HaulCollectionController (UICollectionViewLayout layout) : base (layout)
    {
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        CollectionView.BackgroundColor = UIColor.Clear;
        CollectionView.RegisterClassForCell (typeof(HaulCollectionCell), HaulCollectionCell.Key);
    }

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

    public override int GetItemsCount (UICollectionView collectionView, int section)
    {
        return 6;
    }

    public override UICollectionViewCell GetCell (UICollectionView collectionView, NSIndexPath indexPath)
    {
        var cell = collectionView.DequeueReusableCell (HaulCollectionCell.Key, indexPath) as HaulCollectionCell;
        return cell;
    }

    public override bool ShouldHighlightItem (UICollectionView collectionView, NSIndexPath indexPath)
    {
        return false;
    }
}

UICollectionViewCell:

public class HaulCollectionCell : UICollectionViewCell
{
    public static readonly NSString Key = new NSString ("HaulCollectionCell");

    public UILabel Retailer { get; set; }
    public UILabel Brand { get; set; }
    public UILabel ItemName { get; set; }

    [Export ("initWithFrame:")]
    public HaulCollectionCell (RectangleF frame) : base (frame)
    {
        BackgroundColor = UIColor.Cyan;

        var infoOverlay = new UIView (new RectangleF(0, Frame.Height-60, Frame.Width, 55)) {
            BackgroundColor = UIColor.FromRGBA(255, 255, 255, 153)
        };

        Retailer = new UILabel (new RectangleF(15,10,100,22)) {
            Font = ViewHelpers.GetFont(20, false),
            Text = "DICK'S"
        };

        Brand = new UILabel (new RectangleF(Retailer.IntrinsicContentSize.Width + 10, 10, 100,22)) {
            Font = ViewHelpers.GetFont(20, true),
            Text = "Nike"
        };

        ItemName = new UILabel (new RectangleF(15, Brand.Frame.Bottom + 5, 200,30)) {
            Font = ViewHelpers.GetFont(26, false),
            Text = "Windrunner Tech Fleece"
        };

        infoOverlay.AddSubviews (Retailer,Brand,ItemName);

        ContentView.Add (infoOverlay);
    }
}

无法弄清楚为什么我无法显示细胞......

0 个答案:

没有答案