How to change background color of a whole section in UICollectionView?

后端 未结 10 928
暗喜
暗喜 2020-12-13 18:21

In UICollectionView, I want to give the whole section a uniform background color, instead of for a single cell or for the whole collection view.

I don\'t see any de

10条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 18:43

    I have changed the background color of each section in a very simple manner in the following method: But I was not sure whether it is the right thing to do. But it did work.

    - (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;
    }
    

提交回复
热议问题