2016-09-27 2 views
0

Ich habe seltsames Problem beim Scrollen einer meiner CollectionViews. Wenn ich indexPath.row drucke, geht das ok von 0..6, aber dann ist das letzte indexPath.row 3, und wenn ich dann zurückscrolle, stürzt es entweder mit excbadadress ab oder druckt 1, 2, 4 in zufälliger Reihenfolge. Ich nehme an, dass ich einen Fehler in meinem Code habe und verstehe wirklich nicht wo es ist. Hier ist mein Code: VC:UICollectionView indexPath.row seltsames Verhalten

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize { 
    if collectionView == mainEventsCollectionView { 
     return CGSize(width: UIScreen.main.bounds.width - 40, height: 180) 
    } else { 
     return CGSize(width: 150, height: 200) 
    } 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    if collectionView == mainEventsCollectionView { 
     return 3 
    } else { 
     return 7 
    } 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    if collectionView == mainEventsCollectionView { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mainEventsCell", for: indexPath) as! MainEventCollectionViewCell 
     cell.eventTitle.text = "Hello world".uppercased() 
     cell.setupCell() 
     return cell 
    } else { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "importantEventsCell", for: indexPath) as! ImportantEventsCollectionViewCell 
     print(indexPath.row) 
     cell.setupCell() 
     return cell 
    } 
} 

Cell:

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
    self.roundCorners(.allCorners, radius: 6, borderColor: .clear, borderWidth: 0) 
} 

func setupCell() { 
    self.content.backgroundColor = colorWithAlpha(.black, alpha: 0.7) 
    self.eventDate.textColor = .white 
    self.eventTime.textColor = .white 
    self.eventName.textColor = .white 
    if self.content.layer.sublayers!.count > 3 { 
     self.content.layer.sublayers!.removeLast() 
    } 
    if self.eventDate.text == "Today" { 
     self.content.backgroundColor = .clear 
     DispatchQueue.main.async(execute: { 
      self.content.drawGradient(colors: [UIColor(red: 250/255, green: 217/255, blue: 97/255, alpha: 0.7).cgColor, UIColor(red: 255/255, green: 135/255, blue: 67/255, alpha: 0.7).cgColor], locations: [0, 1]) 
     }) 
     self.eventDate.textColor = .black 
     self.eventTime.textColor = .black 
     self.eventName.textColor = .black 
    } 
} 
+0

Ich kann nichts sehen, was dies verursachen würde, ist dies auf GitHub? –

+0

Scrollt das Scrollen für mainEventsCell oder wichtigEventCell? – user3608500

+0

nein es ist nicht, und wichtigEventsCell, sollte auch hinzufügen, dass diese CollectionViews im Header von TableView aber dataSource und Delegate an ViewController sind – JuicyFruit

Antwort

0

Ok, das Problem war, dass ich versucht habe, Gradient auf einen UIView anzuwenden, die Elemente hatte (UILables). Was ich tun musste, ist, eine Unteransicht zu UIView hinzuzufügen und einen Gradienten darauf anzuwenden.