gpt4 book ai didi

ios - 尝试在 UICollectionView 中显示标题时未调用 GetViewForSupplementaryElement

转载 作者:行者123 更新时间:2023-12-01 18:48:45 24 4
gpt4 key购买 nike

我正在尝试设置 UICollectionView在我现有的UIViewController 中.除了为每个部分显示一个标题外,一切正常 - 我无法弄清楚我做错了什么。

我在 UIViewController 中的代码启动 Collection View :

public partial class ViewController : UIViewController
{
//...

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

CollectionView_Outlet.RegisterClassForCell(typeof(ModifierCell), ModifierCell.CellID);
CollectionView_Outlet.RegisterClassForSupplementaryView (typeof(Header), UICollectionElementKindSection.Header, Header.HeaderId);
CollectionView_Outlet.ShowsHorizontalScrollIndicator = false;
CollectionView_Outlet.Source = new ModifiersSource(this);
CollectionView_Outlet.BackgroundColor = UIColor.White;
CollectionView_Outlet.ReloadData();
}

//...
}

然后我创建了 UICollectionViewSource 的子类:
public class ModifiersSource : UICollectionViewSource
{
ViewController senderVC;

public ModifiersSource(ViewController sender)
{
senderVC = sender;
}

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

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

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


public override UICollectionReusableView GetViewForSupplementaryElement(UICollectionView collectionView, NSString elementKind, NSIndexPath indexPath)
{
var headerView = (Header)collectionView.DequeueReusableSupplementaryView (elementKind, Header.HeaderId, indexPath);
headerView.Text = "Supplementary View";
return headerView;
}

}

最后创建:
public class Header : UICollectionReusableView
{
public static NSString HeaderId = new NSString("UserSource1");
UILabel label;

public string Text {
get {
return label.Text;
}
set {
label.Text = value;
SetNeedsDisplay ();
}
}

[Export ("initWithFrame:")]
public Header (RectangleF frame) : base (frame)
{
label = new UILabel (){
Frame = new RectangleF(0,0,300,50),
BackgroundColor = UIColor.Red};
AddSubview (label);
BackgroundColor = UIColor.White;
}
}

我在 GetViewForSupplementaryElement 上放了一个断点方法,但它永远不会被调用。我还在 StoryBoard 中设置了以下内容:

XCode Screenshot

我错过了什么?!

最佳答案

在多次尝试无法使上述工作后,我手动设置 UICollectionViewFlowLayout同时启动 UIContainerView .似乎成功了,但不知道为什么它没有从我的 StoryBoard 中获取设置.这是我的工作代码:

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

CollectionView_Outlet.RegisterClassForCell(typeof(ModifierCell), ModifierCell.CellID);
CollectionView_Outlet.RegisterClassForCell(typeof(ItemOptionCell), ItemOptionCell.CellID);
CollectionView_Outlet.RegisterClassForSupplementaryView (typeof(Header), UICollectionElementKindSection.Header, Header.HeaderId);
CollectionView_Outlet.ShowsHorizontalScrollIndicator = false;

//This is the new bit I added:
var layout = new UICollectionViewFlowLayout ();
layout.HeaderReferenceSize = new CGSize (300, 40);
CollectionView_Outlet.SetCollectionViewLayout (layout, false);

CollectionView_Outlet.Source = new ModifiersSource(this);
CollectionView_Outlet.ReloadData();
}

关于ios - 尝试在 UICollectionView 中显示标题时未调用 GetViewForSupplementaryElement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32863590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com