Ich bin eine Anwendung mit Xamarin.iOS erstellen. Zuerst möchte ich eine Liste von Kategorien in einer GridView anzeigen. Dazu habe ich CollectionView
mit Custom Cell Layout
verwendet. Um das benutzerdefinierte Zellenlayout zu erstellen, habe ich "CollectioViewCell" von der Schnittstelle verwendet, die es mir erlaubt, das Layout mit Drag & Drop zu gestalten.CollectionView mit customCell Layout funktioniert nicht
Unten ist der Code:
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;
}
}
Aber wenn ich die App laufen sie wirft mir einen Fehler:
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
Irgendwelche Vorschläge?
Was bei SectionViewCell.cs ist: 26? entweder imageUrl, sectionName oder der Text scheint null zu sein. – Pilatus