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