【发布时间】:2013-06-10 16:01:24
【问题描述】:
我在情节提要中构建了一个UICollectionView,并在视图控制器中实现了所有必需的数据源和委托方法。在情节提要中,我检查了集合视图上的Section Header 属性,并将标题视图的类设置为UICollectionResusableView 的子类(在情节提要中)。
从这里开始,我通过故事板将两个 UI 元素拖到标题视图中——一个标签和一个分段控件:
程序执行时,标签出现在集合视图的标题视图中(无需实际代码),但分段控件不会。然而,当一个分段控件被拖到一个典型的UIView 上时,它会显示出来并且是可操作的,不需要任何代码。即使通过IBOutlet 中的代码实例化,分段控件也不会出现。
为什么在典型的UIView 中的集合视图的标题上看不到分段控件,为什么标签显示没有问题?
更新
这是自定义标题视图的 init 方法,我尝试在其中以编程方式添加分段控件(而不是在情节提要中):
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"One", @"Two", nil]];
[_segmentedControl setFrame:CGRectMake(0, 0, 100, 50)];
[_segmentedControl addTarget:self action:@selector(segmentedControlChanged:) forControlEvents:UIControlEventValueChanged];
[self addSubview:_segmentedControl];
}
return self;
}
根据要求,这里是主视图控制器中的-[UICollectionReusableView viewForSupplementaryElementOfKind:] 方法:
- (UICollectionReusableView *)collectionView:(UICollectionView *)cv viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
GalleryHeader *headerView = [cv dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
return headerView;
}
【问题讨论】:
-
你实现
uicollectionviewdatasource方法collectionView:viewForSupplementaryElementOfKind:atIndexPath:了吗? -
是的,我做到了。我也会发布代码。
-
好的,那就等编辑吧
标签: ios storyboard uicollectionview uicollectionreusableview