2016-10-19 11 views
7

Próbuję zaimplementować UICollectionViewFlowLayout lub UICollectionViewLayout - w żaden sposób nie jest wywoływany layoutAttributesForItem.UICollectionViewLayout i układ metodyAtrybutyForItem nigdy nie jest nazywane

Widzę od innych, że nazywają się layoutAttributesForItem z self.layoutAttributesForItem.

Jak this flowout przykład:

Stworzyłem Playground projekt można patrzeć. Ogólnie próbuję po prostu powiększyć widok centrum.

+0

Wiem, że to bardzo trywialne pytanie, ale śmiem prosić go. Czy przypisałeś własną klasę do układu swojej kolekcjiZobacz poprawnie? – Adeel

+0

Dzięki za pytanie :-) Jeśli masz czas, spójrz na to Playground: https://github.com/JCzz/CollectionView Ogólnie próbuję tylko skalować Widok na środku ekranu. –

+0

@ ChrisG. Mam ten sam problem. Wszystko wydaje się w porządku, z wyjątkiem "LayoutAttributesForItem" nie uruchamia się wcale. Nie jestem pewien dlaczego. – jnel899

Odpowiedz

1

próbować używać go przez Sub-classing UICollectionViewFlowLayout

let flowLayout = LeftAgignedFlowLayout() 
flowLayout.minimumLineSpacing = 18 
flowLayout.minimumInteritemSpacing = 8 
flowLayout.scrollDirection = .vertical 
self.collectionViewLayout = flowLayout 



class LeftAgignedFlowLayout : UICollectionViewFlowLayout {   

    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 
     let arr = super.layoutAttributesForElements(in: rect)! 
     return arr.map { 
      atts in 

      var atts = atts 
      if atts.representedElementCategory == .cell { 
       let ip = atts.indexPath 
       atts = self.layoutAttributesForItem(at:ip)! 
      } 
      return atts 
     } 
    } 

    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? { 
     var atts = super.layoutAttributesForItem(at:indexPath)! 
     if indexPath.item == 0 { 
      return atts 

     } 
     if atts.frame.origin.x - 1 <= self.sectionInset.left { 
      return atts 

     } 
     let ipPv = IndexPath(item:indexPath.row-1, section:indexPath.section) 
     let fPv = self.layoutAttributesForItem(at:ipPv)!.frame 
     let rightPv = fPv.origin.x + fPv.size.width + self.minimumInteritemSpacing 
     atts = atts.copy() as! UICollectionViewLayoutAttributes 
     atts.frame.origin.x = rightPv 
     return atts 
    } 

} 
+0

Więc to, co naprawdę proponujesz, to to, że nazywamy 'layoutAttributesForItem (at:)' siebie, zamiast polegać na 'UICollectionViewFlowLayout' i oczekiwać od niego wywołania zwrotnego? :/ –

+0

@MohammadAbdurraafay tak, również jeśli używamy niestandardowego flowlayout, to wywoła to przesłonięcie func. –

+0

To jest najlepszy przykład w Internecie (który można znaleźć) layoutAttributesForItem w swift. –

Powiązane problemy