CollectionView单元格不会更改backgroundColor

时间:2016-07-19 16:30:49

标签: ios swift uicollectionview cell uicolor

正如标题所说,我在使用UltraVisualLayout类时遇到了一些麻烦。当我将背景颜色更改为我的单元格(使用UltraVisualLayout作为collectionView中的自定义布局)时,单元格似乎没有颜色,而是如果我更改为标准流程布局,则单元格会更改其背景颜色。 我使用这些线为细胞着色:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "squadraCell", for: indexPath)

    switch (indexPath.row % 5)
    {
        // ho provato anche con cell.contentView.backgroundColor ma non funziona nemmeno così
        case 0: cell.contentView.backgroundColor = UIColor.yellow()
        case 1: cell.contentView.backgroundColor = UIColor.orange()
        case 2: cell.contentView.backgroundColor = UIColor.red()
        case 3: cell.contentView.backgroundColor = UIColor.green()
        case 4: cell.contentView.backgroundColor = UIColor.blue()
        default: break
    }

    return cell
}

这是我的自定义布局(https://www.raywenderlich.com/99087/swift-expanding-cells-ios-collection-views

//
//  UltravisualLayout.swift
//  RWDevCon
//
//  Created by Mic Pringle on 27/02/2015.
//  Copyright (c) 2015 Ray Wenderlich. All rights reserved.
//

import UIKit

/* The heights are declared as constants outside of the class so they can be easily referenced elsewhere */
struct UltravisualLayoutConstants
{
    struct Cell
    {
        /* The height of the non-featured cell */
        static let standardHeight: CGFloat = 100
        /* The height of the first visible cell */
        static let featuredHeight: CGFloat = 280
    }
}

class UltravisualLayout: UICollectionViewLayout
{

    // MARK: Properties and Variables

    /* The amount the user needs to scroll before the featured cell changes */
    let dragOffset: CGFloat = 180.0

    var cache = [UICollectionViewLayoutAttributes]()

    /* Returns the item index of the currently featured cell */
    var featuredItemIndex: Int
    {
        get
        {
            /* Use max to make sure the featureItemIndex is never < 0 */
            return max(0, Int(collectionView!.contentOffset.y / dragOffset))
        }
    }

    /* Returns a value between 0 and 1 that represents how close the next cell is to becoming the featured cell */
    var nextItemPercentageOffset: CGFloat
    {
        get
        {
            return (collectionView!.contentOffset.y / dragOffset) - CGFloat(featuredItemIndex)
        }
    }

    /* Returns the width of the collection view */
    var width: CGFloat
    {
        get
        {
            return collectionView!.bounds.width
        }
    }

    /* Returns the height of the collection view */
    var height: CGFloat
    {
        get
        {
            return collectionView!.bounds.height
        }
    }

    /* Returns the number of items in the collection view */
    var numberOfItems: Int
    {
        get
        {
            return collectionView!.numberOfItems(inSection: 0)
        }
    }

    // MARK: UICollectionViewLayout

    /* Return the size of all the content in the collection view */
    override func collectionViewContentSize() -> CGSize
    {
        let contentHeight = (CGFloat(numberOfItems) * dragOffset) + (height - dragOffset)

        return CGSize(width: width, height: contentHeight)
    }

    override func prepare()
    {
        cache.removeAll(keepingCapacity: false)

        let standardHeight = UltravisualLayoutConstants.Cell.standardHeight
        let featuredHeight = UltravisualLayoutConstants.Cell.featuredHeight

        var frame = CGRect.zero
        var y: CGFloat = 0

        for item in 0..<numberOfItems
        {
            let indexPath = NSIndexPath(item: item, section: 0)
            let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath as IndexPath)
            /* Important because each cell has to slide over the top of the previous one */
            attributes.zIndex = item
            /* Initially set the height of the cell to the standard height */
            var height = standardHeight
            if indexPath.item == featuredItemIndex {
                /* The featured cell */
                let yOffset = standardHeight * nextItemPercentageOffset
                y = collectionView!.contentOffset.y - yOffset
                height = featuredHeight
            } else if indexPath.item == (featuredItemIndex + 1) && indexPath.item != numberOfItems
            {
                /* The cell directly below the featured cell, which grows as the user scrolls */
                let maxY = y + standardHeight
                height = standardHeight + max((featuredHeight - standardHeight) * nextItemPercentageOffset, 0)
                y = maxY - height
            }
            frame = CGRect(x: 0, y: y, width: width, height: height)
            attributes.frame = frame
            cache.append(attributes)
            y = frame.maxY
        }
    }

    /* Return all attributes in the cache whose frame intersects with the rect passed to the method */
    func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]?
    {
        var layoutAttributes = [UICollectionViewLayoutAttributes]()
        for attributes in cache
        {
            if attributes.frame.intersects(rect)
            {
                layoutAttributes.append(attributes)
            }
        }

        return layoutAttributes
    }

    /* Return the content offset of the nearest cell which achieves the nice snapping effect, similar to a paged UIScrollView */
    override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint
    {
        let itemIndex = round(proposedContentOffset.y / dragOffset)
        let yOffset = itemIndex * dragOffset

        return CGPoint(x: 0, y: yOffset)
    }

    /* Return true so that the layout is continuously invalidated as the user scrolls */
    func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool
    {
        return true
    }
}

这是我的Xcode项目https://www.dropbox.com/sh/7xcu118a9zb7673/AAD6t5zhIgKC5fUUpUTb-yA1a?dl=0

你能帮帮我吗?

0 个答案:

没有答案