2016-09-27 72 views
0

我有一个奇怪的问题,而滚动我的一个collectionViews。当我正在打印indexPath.row从0..6开始可以,但最后的indexPath.row是3,然后如果我向后滚动它,可以用excbadadress崩溃或以随机顺序打印1,2,4。我认为我有我的代码和错误,但真的不明白它在哪里。 这里是我的代码: VC:UICollectionView indexPath.row奇怪的行为

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 
    } 
} 

细胞:

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

我看不到任何会导致这种情况的东西,这是在github上吗? –

+0

正在滚动崩溃的mainEventsCell或importantEventsCell? – user3608500

+0

不,它不是,而importantEventsCell,还应该补充说,这些collectionViews在tableView的头部,但dataSource和委托给ViewController – JuicyFruit

回答

0

好了,问题是,我试图运用渐变到UIView,其中有元素(UILables)。我所要做的是将一个子视图添加到UIView并对其应用渐变。