UICollectionView由代码不显示单元格

时间:2016-10-18 13:58:41

标签: c# ios xamarin uicollectionview uicollectionviewcell

我有一个主ViewController,其中包含使用Storyboard创建的多个视图。当客户端单击一个单元格时,应显示其中包含另一个UICollectionView的弹出窗口。

第二个视图由ViewDidLoad中的代码创建,但仅在客户端点击第一个视图(在Storyboard中创建)中的单元格时显示。

视图显示在所有内容的顶部,顶部有图像和标签。 CollectionView也在那里,但缺少单元格。我使用了断点,它从不更新单元格。

非常欢迎你的帮助。

AirLineViewController.cs中的

UIImageView cardLogo;
    UILabel cardTitle;
    List<UIImage> cardImages;
    List<string> cardLabels;
    UICollectionView cardView;
    CGRect cardFrame;
    UICollectionViewLayout cardViewLayout;
    List<CardCellData> cardCellData;
    CardSource cardSource;

在ViewDidLoad中我调用我的函数initCardView();

private void initCardView()
    {
        cardFrame = new CGRect(10f,
            UIScreen.MainScreen.Bounds.Height * 0.2f,
            UIScreen.MainScreen.Bounds.Width - 20f,
            UIScreen.MainScreen.Bounds.Height * 0.6f);

        creditCardView = new UIView(cardFrame);
        creditCardView.BackgroundColor = UIColor.White;

        cardTitle = new UILabel(new CGRect(40, 0, 100, 35));
        cardTitle.MinimumFontSize = 20f;
        cardTitle.Text = "Air Canada";

        cardLogo = new UIImageView(new CGRect(2, 2, 35, 35));
        cardLogo.ContentMode = UIViewContentMode.ScaleAspectFit;
        cardLogo.Image = UIImage.FromFile("temp/airline_ac");

        cardCellData = new List<CardCellData>();
        cardCellData.Add(new CardCellData(UIImage.FromFile("temp/AirCanada"), "un"));
        cardCellData.Add(new CardCellData(UIImage.FromFile("temp/AirCanada"), "deux"));
        cardCellData.Add(new CardCellData(UIImage.FromFile("temp/AirCanada"), "trois"));
        cardCellData.Add(new CardCellData(UIImage.FromFile("temp/AirCanada"), "quatre"));
        cardSource = new CardSource(cardCellData, this);

        //CView
        cardViewLayout = new UICollectionViewLayout();
        cardView = new UICollectionView(new CGRect(2f,
            45f,
            cardFrame.Width - 4f,
            cardFrame.Height - 50f), cardViewLayout);

        cardView.BackgroundColor = UIColor.White;

        cardView.RegisterClassForCell(typeof(CardCell), (NSString)CardCell.CELLID);
        cardView.DataSource = cardSource;
        cardView.ReloadData();

        creditCardView.AddSubview(cardTitle);
        creditCardView.AddSubview(cardLogo);
        creditCardView.AddSubview(cardView);


        cardView.ReloadData();
    }

要调用的功能以显示视图

 public void displayCardView(int itemIndex)
    {
        //cardView.Source = new CardSource(cardCellData, this);
        this.Add(creditCardView);
        //cardView.ReloadData();
    }

班级CardSource

internal class CardSource : UICollectionViewSource
{
    protected List<CardCellData> items;
    protected NSString cellIdentifier = (NSString)CardCell.CELLID;
    AirLineViewController airLineViewController;

    public CardSource(List<CardCellData> items, AirLineViewController airLineViewController)
    {
        this.items = items;
        this.airLineViewController = airLineViewController;

    }

    public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
    {
        var cell = (CardCell)collectionView.DequeueReusableCell((NSString)CardCell.CELLID, indexPath);

        //cell.img.Image = items[indexPath.Row].image;
        //cell.footerLabel.Text = items[indexPath.Row].footer;

        cell.updateCell(items[indexPath.Row].image, items[indexPath.Row].footer);

        return cell;
    } 

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

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

    public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
    {
        airLineViewController.displayCardView(indexPath.Row);
        //collectionView.DeselectItem(indexPath, true);
    }
}

CardCellData类

public class CardCellData
{
    public UIImage image;
    public string footer;

    public CardCellData(UIImage image, string footer)
    {
        this.image = image;
        this.footer = footer;
    }
}

最后是CardCell类

public class CardCell : UICollectionViewCell
{
    public UIImageView img;
    public UILabel footerLabel;
    public CGRect rect;
    public const string CELLID = "cardViewCell";

    [Export("initWithFrame:")]
    public CardCell(System.Drawing.RectangleF frame) : base(frame){
        this.Initialize();
    }

    public void updateCell(UIImage image, string footer)
    {
        //img = new UIImageView(image);
        img.Image = image;
        footerLabel.Text = footer;
    }

    private void Initialize()
    {

        rect = new CGRect(0f, 0f, 80f, 70f);
        //BackgroundView = new UIView { BackgroundColor = UIColor.Green };

        //ContentView.BackgroundColor = UIColor.White;
        // this.BackgroundView.Frame = rect;
        //this.SelectedBackgroundView.Frame = rect;
        //this.ContentView.Frame = rect;

        img = new UIImageView(UIImage.FromFile("Flag/Flag_0"));
        img.Center = ContentView.Center;
        img.ContentMode = UIViewContentMode.ScaleAspectFit;
        img.Image = UIImage.FromFile("Flag/Flag_0");
        //img.AutoresizingMask = ~UIViewAutoresizing.None;

        footerLabel = new UILabel(new CGRect(0f, 50f, 80f, 70f));

        this.ContentView.AddSubview(img);
        this.ContentView.AddSubview(footerLabel);;
    }
}

0 个答案:

没有答案