2015-10-07 63 views
0

我正在使用集合視圖來顯示從Web服務獲取的數據。我也有一個補充視圖(標題),其中包含一個UIImageView和一個標籤。 UIImageView動畫顯示圖像數組。當我滾動視圖時出現問題。當標題隱藏,然後向上滾動顯示時,應用程序會暫時凍結。UICollectionView:不要重新加載補充視圖

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新手。

回答

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 
} 
} 
相關問題