【问题标题】:Terminating app due to uncaught exception 'NSInvalidArgumentException, reason: 'attempt to scroll to invalid index path由于未捕获的异常“NSInvalidArgumentException,原因:”尝试滚动到无效的索引路径而终止应用程序
【发布时间】:2017-01-07 13:06:11
【问题描述】:

我在UITableviewCell 中添加一个UICollectionView,然后添加此代码为我的UICollectionViewCell 设置动画:

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)

}

}

当我不滚动表格时代码工作正常,但是当我滚动表格时它会崩溃并抛出此错误:

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

提前致谢。

【问题讨论】:

  • 我认为您遗漏了一些可以更容易发现问题的代码,您能否展示一下您是如何填充数据源的?
  • 这个错误通常是因为它试图滚动到不存在的东西,所以 indexpath 不存在
  • @bolnad 更新了我的完整代码,我知道当我们尝试滚动查找不存在的索引路径时会出现此问题。
  • @dirtydanee,我已经探讨过这个问题,但该问题的解决方案对我没有帮助。

标签: ios swift


【解决方案1】:

查看您提供的代码后,您似乎想在 UITableView 上自动滚动集合视图。当您通过在cellForRowAtIndexPath 中调用configureCell 填充UITableView 的单元格时,这似乎会发生。

看起来正在发生的事情是,当您滚动时,在您滚动浏览的每个单元格上都会调用 cellForRowAtIndexPath,并触发 configureCell。在 configureCell 中配置一个 Timer 以开始重复动画。

我看不到你在哪里使那个计时器失效或在你完成那个单元后停止它。

您可以通过注释掉计时器并查看问题是否消失来测试这个理论。

如果是这个问题,那么解决这个问题的最简单方法是实现 UITableViewCell 的prepareForReuse() 方法并使计时器无效。

这是发给prepareForReuse 的 Apple 文档

【讨论】:

  • 谢谢,你节省了我的时间 :)
猜你喜欢
  • 2018-09-06
  • 1970-01-01
  • 2018-11-29
  • 2018-04-05
  • 2013-04-27
  • 2021-09-17
  • 2012-07-12
  • 2021-02-03
  • 1970-01-01
相关资源
最近更新 更多