UICollectionView:不要重新加载补充视图

时间:2015-10-07 11:07:54

标签: ios swift uicollectionview

我正在使用集合视图来显示从Web服务获取的数据。我还有一个补充视图(标题),其中包含一个UIImageView和一个标签。 UIImageView动画显示一系列图像。滚动视图时出现问题。当标题被隐藏然后向上滚动显示它时,应用程序会暂时冻结。

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {


    let headerView = categoryView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "bannerHeader", forIndexPath: indexPath) as! HeaderBanner



    print("Got into header")
    print("THE NUMBER OF AD ITEMS IS: \(self.adItems.count)")

    var sliderImages = [UIImage]()
    var imageAddressArray = [String]()


    if(self.adItems.count>0) {
        print("AD ITEMS IS GREATER THAN 0")
        for i in 0..<self.adItems.count {
            imageAddressArray.append(URLEncoder.encodeURL(self.adItems[i].filePath!))

        }

        dispatch_async(dispatch_get_main_queue(), {
            AdsImageDataFetch.fetchImageData(imageAddressArray) { result -> () in
                sliderImages = result
                self.animateImageView(headerView.bannerImage, images: sliderImages, label: headerView.bannerLabel)
            }
        })


    }

    return headerView
}

我想我已经正确地做到了这一点。所以,我想知道在滚动发生时是否有任何方法不加载标题。 iOS和Swift新手。

1 个答案:

答案 0 :(得分:0)

由于我无法找到解决方案,因此我使用了浮动标题视图,以便每次滚动时都不会刷新。对于其他想要在Swift 2.0中使用浮动标题视图的人。这是代码:

class StickyHeaderFlowLayout: UICollectionViewFlowLayout {

    override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
    // Return true so we're asked for layout attributes as the content is scrolled
        return true
    }

    override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        // Get the layout attributes for a standard UICollectionViewFlowLayout
        var elementsLayoutAttributes = super.layoutAttributesForElementsInRect(rect)
        if elementsLayoutAttributes == nil {
            return nil
        }


        // Define a struct we can use to store optional layout attributes in a dictionary
        struct HeaderAttributes {
            var layoutAttributes: UICollectionViewLayoutAttributes?
        }
        var visibleSectionHeaderLayoutAttributes = [Int : HeaderAttributes]()


    // Loop through the layout attributes we have
    for (index, layoutAttributes) in (elementsLayoutAttributes!).enumerate() {
        let section = layoutAttributes.indexPath.section

        switch layoutAttributes.representedElementCategory {
        case .SupplementaryView:
            // If this is a set of layout attributes for a section header, replace them with modified attributes
            if layoutAttributes.representedElementKind == UICollectionElementKindSectionHeader {
                let newLayoutAttributes = layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader, atIndexPath: layoutAttributes.indexPath)
                elementsLayoutAttributes![index] = newLayoutAttributes!

                // Store the layout attributes in the dictionary so we know they've been dealt with
                visibleSectionHeaderLayoutAttributes[section] = HeaderAttributes(layoutAttributes: newLayoutAttributes)
            }

        case .Cell:
            // Check if this is a cell for a section we've not dealt with yet
            if visibleSectionHeaderLayoutAttributes[section] == nil {
                // Stored a struct for this cell's section so we can can fill it out later if needed
                visibleSectionHeaderLayoutAttributes[section] = HeaderAttributes(layoutAttributes: nil)
            }

        case .DecorationView:
            break
        }
    }

    // Loop through the sections we've found
    for (section, headerAttributes) in visibleSectionHeaderLayoutAttributes {
        // If the header for this section hasn't been set up, do it now
        if headerAttributes.layoutAttributes == nil {
            let newAttributes = layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader, atIndexPath: NSIndexPath(forItem: 0, inSection: section))
            elementsLayoutAttributes!.append(newAttributes!)
        }
    }

    return elementsLayoutAttributes
}





override func layoutAttributesForSupplementaryViewOfKind(elementKind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
    // Get the layout attributes for a standard flow layout
    let attributes = super.layoutAttributesForSupplementaryViewOfKind(elementKind, atIndexPath: indexPath)

    // If this is a header, we should tweak it's attributes
    if elementKind == UICollectionElementKindSectionHeader {
        if let fullSectionFrame = frameForSection(indexPath.section) {
            let minimumY = max(collectionView!.contentOffset.y + collectionView!.contentInset.top, fullSectionFrame.origin.y)
            let maximumY = CGRectGetMaxY(fullSectionFrame) - headerReferenceSize.height - collectionView!.contentInset.bottom

            attributes!.frame = CGRect(x: 0, y: min(minimumY, maximumY), width: collectionView!.bounds.size.width, height: headerReferenceSize.height)
            attributes!.zIndex = 1
        }
    }

    return attributes
}





// MARK: Private helper methods

private func frameForSection(section: Int) -> CGRect? {

    // Sanity check
    let numberOfItems = collectionView!.numberOfItemsInSection(section)
    if numberOfItems == 0 {
        return nil
    }

    // Get the index paths for the first and last cell in the section
    let firstIndexPath = NSIndexPath(forRow: 0, inSection: section)
    let lastIndexPath = numberOfItems == 0 ? firstIndexPath : NSIndexPath(forRow: numberOfItems - 1, inSection: section)

    // Work out the top of the first cell and bottom of the last cell
    let firstCellTop = layoutAttributesForItemAtIndexPath(firstIndexPath)!.frame.origin.y
    let lastCellBottom = CGRectGetMaxY(layoutAttributesForItemAtIndexPath(lastIndexPath)!.frame)

    // Build the frame for the section
    var frame = CGRectZero

    frame.size.width = collectionView!.bounds.size.width
    frame.origin.y = firstCellTop
    frame.size.height = lastCellBottom - firstCellTop

    // Increase the frame to allow space for the header
    frame.origin.y -= headerReferenceSize.height
    frame.size.height += headerReferenceSize.height

    // Increase the frame to allow space for an section insets
    frame.origin.y -= sectionInset.top
    frame.size.height += sectionInset.top

    frame.size.height += sectionInset.bottom

    return frame
}
}