UICollectionView では、単一のセルまたはコレクション ビュー全体ではなく、セクション全体に均一な背景色を与えたいと考えています。
それを行うためのデリゲートメソッドが表示されません。何か提案はありますか?
UICollectionView では、単一のセルまたはコレクション ビュー全体ではなく、セクション全体に均一な背景色を与えたいと考えています。
それを行うためのデリゲートメソッドが表示されません。何か提案はありますか?
アイデアは、UICollectionViewLayoutAttributesをオーバーライドして色属性を追加することです。次に、 UICollectionReusableViewをオーバーライドして、ビューの背景に色を適用します。
まだ試していませんが、セルの背後に背景が必要な場合は、装飾ビューを使用する必要があるようです (Books アプリの棚のように)。セクションごとに異なるビューを用意し、デリゲート メソッドを使用して設定できるようにする必要があると思いますlayoutAttributesForDecorationViewOfKind:atIndexPath:
。
コレクション ビューでは、すべてのセクションに補足ビューを含めることができるため、セクションごとに補足ビューを配置し、背景色をセクション セルではなく補足ビューに設定します。それが役立つことを願っています。
このデフォルトのUICollectionViewDelegateのメソッドを使用するだけで非常に簡単です。
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
print(indexPath.item)
let evenSectionColor = UIColor.clear
let oddSectionColor = UIColor.white
cell.contentView.backgroundColor = (indexPath.item % 2 == 0) ? evenSectionColor : oddSectionColor
}
次の方法で各セクションの背景色を非常に簡単に変更しました。しかし、それはうまくいきました。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
FamilyCalendarCellItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"calendarItem" forIndexPath:indexPath];
Event *event;
_headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
UICollectionElementKindSectionHeader withReuseIdentifier:@"EventHeader" forIndexPath:indexPath]; //headerView is declared as property of Collection Reusable View class
if(indexPath.section==0) {
cell.backgroundColor=[UIColor orangeColor];
}
else if(indexPath.section==1) {
cell.backgroundColor=[UIColor yellowColor];
}
return cell;
}