2017-01-07 3 views
0

ich auf ungültig bin Hinzufügen eines UICollectionView in UITableviewCell, und dann das Hinzufügen dieser Code mein UICollectionViewCell zu animieren:Beenden app aufgrund nicht abgefangene Ausnahme ‚NSInvalidArgumentException Grund:‘ Versuch zu blättern Indexpfad

let CardCollectionCellId = "CardCollectionCell" 
let SlideShowTimeInterval = TimeInterval(7) 

protocol CardCellDelegate { 
    func didSelectCard(card:Card) 
} 

class CardTableViewCell: UITableViewCell,UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{ 

    var category:CardCategory! 

    var delegate: CardCellDelegate! 

    var slideShow = true 

    @IBOutlet weak var overlayView: UIVisualEffectView! 

    @IBOutlet weak var collectionView:UICollectionView! 

    func configureCell(){ 

    collectionView.register(UINib(nibName: "CardCollectionCell", bundle: nil), forCellWithReuseIdentifier: CardCollectionCellId) 

    initialzeTimerForCategory(category: category) 

    collectionView.reloadData() 
} 


// TODO:Ideally It should run when cell is visible 
func initialzeTimerForCategory(category:CardCategory){ 

    if category.isSlideShowOn && slideShow { 

      var rowIndex = 0 
      Timer.scheduledTimer(withTimeInterval: SlideShowTimeInterval, repeats: true) { (Timer) in 

       let items = self.collectionView.numberOfItems(inSection: 0) 

       if items > 0{ 
        self.collectionView.scrollToItem(at: IndexPath(item: rowIndex, section: 0), at: UICollectionViewScrollPosition.centeredHorizontally, animated: false) 

        rowIndex = rowIndex < (items - 1) ? (rowIndex + 1) : 0 

       } 
       print("=========>",items,rowIndex) 

      } 
     } 

} 


// MARK: UICollectionViewDataSource 
func numberOfSections(in collectionView: UICollectionView) -> Int { 
    return 1 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return self.category.cards.count 
} 


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let card = self.category.cards[indexPath.row] 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CardCollectionCellId, for: indexPath) as! CardCollectionCell 

    cell.configureCell(card: card) 
    return cell 

} 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    self.delegate.didSelectCard(card: self.category.cards[indexPath.row]) 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    let width = UIScreen.main.bounds.width 
    return CGSize(width: width - 20, height: 350) 

} 

}

-Code funktioniert gut, wenn ich nicht auf den Tisch blättern, aber wenn ich den Tisch blättern dann stürzt und diesen Fehler werfen:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'attempt to scroll to invalid index path: <NSIndexPath: 0x600000231aa0> {length = 2, path = 0 - 1}' 

Vielen Dank im Voraus.

+2

Mögliche Duplikat [ ‚NSInvalidArgumentException‘: Versuch blättern Indexpfad auf ungültig] (http://stackoverflow.com/questions/18201409/nsinvalidargumentexception-attempt-to-scroll- to-invalid-index-path) – dirtydanee

+0

Ich denke, dass Sie Code vermissen, der es einfacher macht, das Problem zu erkennen. Können Sie zeigen, wie Sie die Datenquelle auffüllen? – bolnad

+0

Dieser Fehler ist in der Regel, weil es versucht, zu etwas zu scrollen, das nicht existiert, so dass ein Indexpfad, der nicht vorhanden ist – bolnad

Antwort

0

Nachdem Sie sich den Code angesehen haben, den Sie bereitgestellt haben, sieht es so aus, als ob Sie versuchen, einen CollectionView auf einem UITableView automatisch zu scrollen. Dies scheint zu geschehen, wenn Sie eine Zelle der UITableView durch Aufrufen configureCell in cellForRowAtIndexPath auffüllen.

Wie es aussieht ist passiert, wenn Sie scrollen cellForRowAtIndexPath wird auf jeder Zelle, die Sie durchblättern, durch Auslösen configureCell aufgerufen. Innerhalb configureCell konfigurieren Sie einen Timer, um die Animationen zu wiederholen.

Ich sehe nicht, wo Sie diesen Timer jemals ungültig machen oder stoppen, sobald Sie mit dieser Zelle fertig sind.

Sie könnten diese Theorie testen, indem Sie den Timer auskommentieren und sehen, ob das Problem verschwindet.

Wenn dieses Problem auftritt, dann implementieren Sie am einfachsten die Methode prepareForReuse() der UITableViewCell und machen den Timer ungültig.

hier ist der Apple-Dokumentation zu prepareForReuse

+0

Danke, Du hast meine Zeit gerettet :) – Seema

Verwandte Themen