对于具有单个数据源的单个集合视图,可以为单个单元格类注册两个集合视图单元格原型。
首先,在故事板中,设置
Cell1
作为第一小区原型的重用标识符,以及
Cell2
第二个。他们俩都应该有
PatternViewCell
班
然后,在更改分段控件的值时,重新加载集合视图:
- (IBAction)segmentedControlValueChanged:(id)sender {
[self.collectionView reloadData];
}
将此操作绑定到
Value Changed
事件
然后在
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
方法,您可以根据所选索引选择重用标识符。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSString *identifier = nil;
if (self.segmentedControl.selectedSegmentIndex == 0) {
identifier = @"Cell1";
} else {
identifier = @"Cell2";
}
PatternViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
//configure cell
return cell;
}