2016-04-08 15 views
0

私はXamarin.iOSを使用してアプリケーションを構築しています。最初は、GridViewにカテゴリのリストを表示したいと思います。このため私はCollectionViewCustom Cell Layoutを使用しました。カスタムセルレイアウトを作成するには、インターフェイスから "CollectioViewCell"を使用しました。ドラッグアンドドロップでレイアウトをデザインすることができます。以下customCellレイアウトのCollectionViewが動作しません

コードである:

ViewController.cs

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

    //initialize the data 
    sectionCategories = new List<SectionCategory>(); 
    sectionCategories.Add(new SectionCategory("Lajme", "LajmeUrl")); 
    sectionCategories.Add(new SectionCategory("Lajme", "LajmeUrl")); 
    sectionCategories.Add(new SectionCategory("Lajme", "LajmeUrl")); 
    sectionCategories.Add(new SectionCategory("Lajme", "LajmeUrl")); 
    sectionCategories.Add(new SectionCategory("Lajme", "LajmeUrl")); 
    sectionCategories.Add(new SectionCategory("Lajme", "LajmeUrl")); 
    sectionCategories.Add(new SectionCategory("Lajme", "LajmeUrl")); 
    sectionCategories.Add(new SectionCategory("Lajme", "LajmeUrl")); 

    sectionGrid.RegisterClassForCell(typeof(SectionViewCell), SectionViewCell.Key); 
    sectionGrid.Source = new SectionViewSource (sectionCategories); 
} 

SectionViewCell.cs

public partial class SectionViewCell : UICollectionViewCell 
{ 
    public static readonly NSString Key = new NSString ("SectionViewCell"); 
    public static readonly UINib Nib; 

    static SectionViewCell() 
    { 
     Nib = UINib.FromName ("SectionViewCell", NSBundle.MainBundle); 
    } 

    public SectionViewCell (IntPtr handle) : base (handle) 

    { 

    } 

    public void UpdateCell (string text) 
    { 
     imageUrl.Text = text; 
     sectionName.Text = text; 
    } 
} 

SectionViewCell.designer.cs

[Register ("SectionViewCell")] 
partial class SectionViewCell 
{ 
    [Outlet] 
    [GeneratedCode ("iOS Designer", "1.0")] 
    UILabel imageUrl { get; set; } 

    [Outlet] 
    [GeneratedCode ("iOS Designer", "1.0")] 
    UILabel sectionName { get; set; } 

    void ReleaseDesignerOutlets() 
    { 
     if (imageUrl != null) { 
      imageUrl.Dispose(); 
      imageUrl = null; 
     } 
     if (sectionName != null) { 
      sectionName.Dispose(); 
      sectionName = null; 
     } 
    } 
} 

SectionViewSource.cs

public class SectionViewSource: UICollectionViewSource 
{ 
    public List<SectionCategory> category { get; set; } 

    public SectionViewSource(List<SectionCategory> _category) 
    { 
     category = _category; 
    } 

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

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


    public override UICollectionViewCell GetCell (UICollectionView collectionView, NSIndexPath indexPath) 
    { 

     var cell = (SectionViewCell)collectionView.DequeueReusableCell (SectionViewCell.Key, indexPath); 

     cell.UpdateCell ("lajme"); 

     return cell; 
    } 
} 

しかし、私は、アプリケーションを実行すると、それは私にエラーがスローされます。

System.NullReferenceException: Object reference not set to an instance of an object 
    at MyIkub.SectionViewCell.UpdateCell (System.String text) [0x00008] in /Users/crs/Projects/MyIkub/MyIkub/SectionViewCell.cs:26 
    at MyIkub.SectionViewSource.GetCell (UIKit.UICollectionView collectionView, Foundation.NSIndexPath indexPath) [0x00019] in /Users/crs/Projects/MyIkub/MyIkub/SectionViewSource.cs:33 
    at at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) 
    at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Users/builder/data/lanes/2761/d7cac503/source/maccore/src/UIKit/UIApplication.cs:77 
    at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0001c] in /Users/builder/data/lanes/2761/d7cac503/source/maccore/src/UIKit/UIApplication.cs:60 
    at MyIkub.Application.Main (System.String[] args) [0x00008] in /Users/crs/Projects/MyIkub/MyIkub/Main.cs:12 

任意の提案を?

+0

のですか? imageUrl、sectionNameまたはテキストがnullのようです。 – Pilatus

答えて

2

RegisterClassForCellの代わりにRegisterNibForCellを使用してください。 26:ここでは

sectionGrid.RegisterNibForCell (UINib.FromName("SectionViewCell", NSBundle.MainBundle), SectionViewCell.Key); 

はSectionViewCell.csである何tutorial

+0

保存された私のお尻!それが何であるかあなたは何か手がかりがありますか? XamarinのドキュメントとサンプルはRegisterClassForCellを使用しています。それはバグだと思いますか? –

+0

気にしないでください!なぜ私はこの記事を見つけたのです...それはもっと明らかに笑いたい –

関連する問題