我正在使用Xamarin.iOS构建应用程序。首先,我想在GridView中显示类别列表。为此,我使用CollectionView
和Custom Cell Layout
。为了构建自定义单元格布局,我使用了" CollectioViewCell"从允许我使用拖放设计布局的界面。
以下是代码:
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 :(得分:2)
使用RegisterNibForCell
代替RegisterClassForCell
。
sectionGrid.RegisterNibForCell (UINib.FromName("SectionViewCell", NSBundle.MainBundle), SectionViewCell.Key);
这是tutorial