2016-12-02 2 views
1

CustomCollectionViewLayout을 구현하기 위해 GitHub source을 따르고 있지만 셀 사이에 공백을 추가 할 때 발생하지만 minimumLineSpacingForSectionAtIndex 같은 기능과 가장자리 인세 트 설정 기능은 대리인을 호출하지 않는 경우에도 설정됩니다. 제 1 열 다음에 15 칸이 필요하고 다른 열 뒤에 10 칸이 필요하기 때문에 어떻게 각 열 사이에 공백을 둘 수 있는지 알려주십시오.Swift CollectionView 사용자 지정 레이아웃 셀 간격

이 내가 지금까지 달성 한 것입니다 :

enter image description here

class DemoGraphicCollectionViewLayout: UICollectionViewLayout { 

    var numberOfColumns : Int = 0 
    var itemAttributes : NSMutableArray! 
    var itemsSize : NSMutableArray! 
    var contentSize : CGSize! 

    override func prepare() { 
     if self.collectionView?.numberOfSections == 0 { 
      return 
     } 

     if (self.itemAttributes != nil && self.itemAttributes.count > 0) { 
      for section in 0..<self.collectionView!.numberOfSections { 
       let numberOfItems : Int = self.collectionView!.numberOfItems(inSection: section) 
       for index in 0..<numberOfItems { 
        if section != 0 && index != 0 { 
         continue 
        } 

        let attributes : UICollectionViewLayoutAttributes = self.layoutAttributesForItem(at: IndexPath(item: index, section: section)) 
        if section == 0 { 
         var frame = attributes.frame 
         frame.origin.y = self.collectionView!.contentOffset.y 
         attributes.frame = frame 
        } 

        if index == 0 { 
         var frame = attributes.frame 
         frame.origin.x = self.collectionView!.contentOffset.x 
         attributes.frame = frame 
        } 
       } 
      } 
      return 
     } 

     if (self.itemsSize == nil || self.itemsSize.count != numberOfColumns) { 
      self.calculateItemsSize() 
     } 

     var column = 0 
     var xOffset : CGFloat = 0 
     var yOffset : CGFloat = 0 
     var contentWidth : CGFloat = 0 
     var contentHeight : CGFloat = 0 
     numberOfColumns = (collectionView?.numberOfSections)! 
     for section in 0..<self.collectionView!.numberOfSections { 
      let sectionAttributes = NSMutableArray() 

      for index in 0..<numberOfColumns { 
       var itemSize = (self.itemsSize[index] as AnyObject).cgSizeValue 
       let indexPath = IndexPath(item: index, section: section) 
       let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath) 
       attributes.frame = CGRect(x: xOffset, y: yOffset, width: (itemSize?.width)!, height: (itemSize?.height)!).integral 

       if section == 0 { 
        itemSize = CGSize(width: 150, height: 30) 
       } 


       if section == 0 && index == 0 { 
        attributes.zIndex = 1024; 
       } else if section == 0 || index == 0 { 
        attributes.zIndex = 1023 
       } 

       if section == 0 { 
        var frame = attributes.frame 
        frame.origin.y = self.collectionView!.contentOffset.y 
        frame.size.height = 30 
        attributes.frame = frame 
       } 
       if index == 0 { 
        var frame = attributes.frame 
        frame.origin.x = self.collectionView!.contentOffset.x 
        attributes.frame = frame 
       } 

       sectionAttributes.add(attributes) 

       xOffset += (itemSize?.width)! 
       column += 1 

       if column == numberOfColumns { 
        if xOffset > contentWidth { 
         contentWidth = xOffset 
        } 

        column = 0 
        xOffset = 0 
        yOffset += (itemSize?.height)! 
       } 
      } 
      if (self.itemAttributes == nil) { 
       self.itemAttributes = NSMutableArray(capacity: self.collectionView!.numberOfSections) 
      } 
      self.itemAttributes .add(sectionAttributes) 
     } 

     let attributes : UICollectionViewLayoutAttributes = (self.itemAttributes.lastObject as AnyObject).lastObject as! UICollectionViewLayoutAttributes 

     contentHeight = attributes.frame.origin.y + attributes.frame.size.height 
     self.contentSize = CGSize(width: contentWidth, height: CGFloat(0)) 
    } 

    override var collectionViewContentSize : CGSize { 
     return self.contentSize 
    } 


    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes { 


     let aa = self.itemAttributes[indexPath.section] as! NSMutableArray 
     return aa[indexPath.row] as! UICollectionViewLayoutAttributes 

    } 

    override func layoutAttributesForElements(in rect: CGRect) ->[UICollectionViewLayoutAttributes]? { 
     var attributes = [UICollectionViewLayoutAttributes]() 



     if let itemAttributes = self.itemAttributes as NSArray as? [[UICollectionViewLayoutAttributes]] { 
      for section in itemAttributes { 
       let filteredArray = section.filter {evaluatedObject in 
        return rect.intersects(evaluatedObject.frame) 
       } 
       attributes.append(contentsOf: filteredArray) 
      } 
     } 
     return attributes 
    } 

    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool { 
     return true 
    } 

    func sizeForItemWithColumnIndex(_ columnIndex: Int) -> CGSize { 


     return CGSize(width: 150, height: 130) 
    } 

    func calculateItemsSize() { 
     self.itemsSize = NSMutableArray(capacity: (collectionView?.numberOfSections)!) 
     for index in 0..<(collectionView?.numberOfSections)! { 
      self.itemsSize.add(NSValue(cgSize: self.sizeForItemWithColumnIndex(index))) 
     } 
    } 
} 
+0

응답하지 않고 투표를하는 이유가 있습니까? –

+0

이 문제가 해결 되었습니까? –

답변

0

글쎄, 내가 타사 라이브러리의 코드를 테스트 한 나는 또한 내 대리자 메서드 호출되지 않은 즉 유사한 문제에 직면 해있다. 이 문제를 조사하면서 나는 다음을 발견했다 :

minimumInteritemSpacingForSectionAtUICollectionViewFlowLayout에 의해 사용되는 UICollectionViewDelegateFlowLayout의 일부이다. CustomCollectionLayoutUICollectionViewLayout의 하위 클래스이며 UICollectionViewFlowLayout이 아닙니다. 왜 대리자 메서드가 호출되지 않습니다.

소스 링크에서 언급 한대로 prepare 방법으로 레이아웃을 조정할 수 있습니다. 불행히도 나는 그것을 구현할 충분한 시간을 얻지 못했지만 당신이 어떤 해결책을 찾을 수 있기를 바랍니다.

Here I found this info

+0

나는 이것도 레이아웃의 가치를 설정하고 그것을 collectionview 레이아웃에 추가하여 시도했지만 역시 작동하지 않습니다. –

관련 문제