41

UICollectionView では、単一のセルまたはコレクション ビュー全体ではなく、セクション全体に均一な背景色を与えたいと考えています。

それを行うためのデリゲートメソッドが表示されません。何か提案はありますか?

4

10 に答える 10

3

アイデアは、UICollectionViewLayoutAttributesをオーバーライドして色属性を追加することです。次に、 UICollectionReusableViewをオーバーライドして、ビューの背景に色を適用します。

https://github.com/strawberrycode/SCSectionBackground

于 2016-09-19T18:10:29.863 に答える
2

まだ試していませんが、セルの背後に背景が必要な場合は、装飾ビューを使用する必要があるようです (Books アプリの棚のように)。セクションごとに異なるビューを用意し、デリゲート メソッドを使用して設定できるようにする必要があると思いますlayoutAttributesForDecorationViewOfKind:atIndexPath:

于 2012-11-28T17:34:19.240 に答える
0

コレクション ビューでは、すべてのセクションに補足ビューを含めることができるため、セクションごとに補足ビューを配置し、背景色をセクション セルではなく補足ビューに設定します。それが役立つことを願っています。

于 2013-12-03T14:06:15.823 に答える
-1

このデフォルトの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

}
于 2019-08-06T22:20:15.673 に答える
-4

次の方法で各セクションの背景色を非常に簡単に変更しました。しかし、それはうまくいきました。

- (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;
}
于 2016-02-29T04:58:12.883 に答える