jeudi 23 juin 2016

Sticky Headers form CustomClass


I am trying to implement sticky headers from a custom class that I made. I found a ton of helpful code on how to implement sticky headers but for some reason mine is not working. Please tell me if you see something I dont

class SecondCustomLayout: UICollectionViewLayout {

let horizontalInsets : CGFloat = 10.0
  let verticalInsets : CGFloat = 10.0
  let minItemWidth : CGFloat = 50.0
  let maxItemWidth : CGFloat = 100.0
  let itemHeight : CGFloat = 50.0
  let itemWidth : CGFloat = 150.0
  let columnCount : CGFloat = 4.0
  var yOffset : CGFloat = 10.0
  var xOffset : CGFloat = 900.0
  var _layoutAttributes = [String : UICollectionViewLayoutAttributes]()
  var _contentSize = CGSizeZero

  override func prepareLayout() {
    // collectionView Header
    let headerIndexPath = NSIndexPath(forItem: 0, inSection: 0)
    let headerAttributes = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind:
        UICollectionElementKindSectionHeader, withIndexPath: headerIndexPath)
    let headerHeight = self.itemHeight * 1.2
    headerAttributes.frame = CGRectMake(0, 0, self.collectionView!.frame.size.height, headerHeight)
    let headerkey = layoutKeyForHeaderAtIndexPath(headerIndexPath)
    _layoutAttributes[headerkey] = headerAttributes
    // collectionView items + sections
    let numberOfSections = self.collectionView!.numberOfSections()
    for section in 0 ..< numberOfSections {
      let numberOfItems = self.collectionView!.numberOfItemsInSection(section)
      for item in 0 ..< numberOfItems {
        let indexPath = NSIndexPath(forItem: item, inSection: section)
        let indexPathAttributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
        // collectionView cell layout
        let itemSize = CGSizeMake(self.itemWidth, self.itemHeight)
        let increaseRow = self.collectionView!.frame.size.width - xOffset <= self.maxItemWidth
        // save indexPath once cells are laid out
        indexPathAttributes.frame = CGRectMake(xOffset, yOffset, itemSize.width,
            itemSize.height)
        let frameKey = layoutKeyForIndexPath(indexPath)
        _layoutAttributes[frameKey] = indexPathAttributes
        // update Offsets
        xOffset += itemSize.width
        if increaseRow && !(item == numberOfItems - 1  && section == numberOfItems - 1 ) {
          yOffset += self.itemHeight
          xOffset = self.horizontalInsets
          }
        }
      }
    yOffset += self.itemHeight
    _contentSize = CGSizeMake(itemWidth * columnCount, yOffset + self.verticalInsets)
    for (_,value) in _layoutAttributes {
      let layoutAtts = value
      if let representedElementKind = layoutAtts.representedElementKind {
        if representedElementKind == UICollectionElementKindSectionHeader {
          let section = layoutAtts.indexPath.section
          let numberOfItemsInSection = collectionView!.numberOfItemsInSection(section)
          if numberOfItemsInSection > 0 {
            let firstCellIndexPath = NSIndexPath(forItem: 0, inSection: section)
            let lastCellIndexPath = NSIndexPath(forItem: max(0, (numberOfItemsInSection - 1)), inSection: section)

            var firstCellAtts : UICollectionViewLayoutAttributes
            var lastCellAtts : UICollectionViewLayoutAttributes

            firstCellAtts = self.layoutAttributesForItemAtIndexPath(firstCellIndexPath)!
            lastCellAtts = self.layoutAttributesForItemAtIndexPath(lastCellIndexPath)!
            var origin = layoutAtts.frame.origin

            origin.y = min(max(yOffset, (CGRectGetMinY(firstCellAtts.frame) - headerHeight)), (CGRectGetMaxY(lastCellAtts.frame) - headerHeight))
            lastCellAtts.frame = CGRect(origin: origin, size: layoutAtts.frame.size)
          }
        }
      }
    }
  }

More information: When I was going through the debugger this line

 if let representedElementKind = layoutAtts.representedElementKind 

was never executed but I am still not sure why?


Aucun commentaire:

Enregistrer un commentaire