Ios insertRowsAtIndexPaths后应用程序崩溃

Ios insertRowsAtIndexPaths后应用程序崩溃,ios,swift,uitableview,Ios,Swift,Uitableview,我试图在用户滚动时,将行动态添加到表视图的底部。其想法是,用户不会注意到这一点,可以滚动“无止境” 当用户到达表的底部40个单元格时,我想在底部绘制100个新单元格 现在当我打电话的时候 tableView.beginUpdates() tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .None) tableView.endUpdates() 应用程序似乎在tableV

我试图在用户滚动时,将行动态添加到表视图的底部。其想法是,用户不会注意到这一点,可以滚动“无止境”

当用户到达表的底部40个单元格时,我想在底部绘制100个新单元格

现在当我打电话的时候

        tableView.beginUpdates()
        tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .None)
        tableView.endUpdates()
应用程序似乎在
tableView.endUpdates()上崩溃

由于未捕获的异常“NSRangeException”而终止应用程序,原因:'***-[\uu NSArrayM objectAtIndex:]:索引10超出边界[0..9]'

我坚信在执行更新之前,
UITableView
的数据提供程序已正确更新。我不知道它为什么会崩溃

为此,我做了一个非常直接的实现。有人知道为什么下面会崩溃吗

private let CellTreshold: Int = 100
var cellCount: Int = 100

@IBOutlet weak var tableView: UITableView!

// UITableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return cellCount
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("MyCellIdentifier") as! UITableViewCell
    cell.index = indexPath.row

    return cell
}

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    if indexPath.row == cellCount - 40 {
        print("indexPath = \(indexPath.row); inserting \(CellTreshold) cells")
        var indexPaths = [NSIndexPath]()

        let beginIndex = cellCount
        let endIndex = cellCount + CellTreshold
        for i in beginIndex ..< endIndex {
            indexPaths.append(NSIndexPath(forRow: i, inSection: 0))
        }
        cellCount += CellTreshold

        tableView.beginUpdates()
        tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .None)
        tableView.endUpdates()
    }
}
private let CellTreshold:Int=100
变量cellCount:Int=100
@IBVAR表格视图:UITableView!
//UITableViewDataSource
func numberOfSectionsInTableView(tableView:UITableView)->Int{
返回1
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回单元格计数
}
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
将cell=tableView.dequeueReusableCellWithIdentifier(“MyCellIdentifier”)设为!UITableViewCell
cell.index=indexPath.row
返回单元
}
func tableView(tableView:UITableView,willDisplayCell单元格:UITableView单元格,forRowAtIndexPath索引路径:nsindepath){
如果indexath.row==cellCount-40{
打印(“indexPath=\(indexPath.row);插入\(CellTreshold)单元格”)
var indexpath=[NSIndexPath]()
让beginIndex=cellCount
让endIndex=cellCount+CellTreshold
对于beginIndex中的i..
我也遇到了同样的问题-它崩溃了,因为代码在willDisplayCell中。感谢@A-live的评论

在scrollViewDidScroll中移动代码后,其工作正常,类似于:

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let offsetY = scrollView.contentOffset.y
    let contentHeght = scrollView.contentSize.height
    let scrollViewHeight = scrollView.frame.size.height

    if (contentHeght - offsetY) < 3*scrollViewHeight {
        //we are less than 3 page screens from the bottom

        let rowNumbers = self.dataModel.getVisibleCells()
        let newRowNumbers = self.dataModel.getMoreCells()
        let newCells = newRowNumbers - rowNumbers

        if newCells > 0 {
            debugPrint("ae.ui adding cells \(newCells)")

            let rowInd = rowNumbers - 1
            var indexPaths = [IndexPath]()
            for i in 1...newCells {
                indexPaths.append(IndexPath(row: rowInd+i, section: 0))
            }

            self.beginUpdates()
            self.insertRows(at: indexPaths, with: .none)
            self.endUpdates()
        }
    }
}
func scrollViewDidScroll(scrollView:UIScrollView){
let offsetY=scrollView.contentOffset.y
让ContentHeight=scrollView.contentSize.height
设scrollViewHeight=scrollView.frame.size.height
if(contentheight-offsetY)<3*滚动视图高度{
//我们从底部开始的屏幕不到3页
让rowNumbers=self.dataModel.getVisibleCells()
让newRowNumbers=self.dataModel.getMoreCells()
设newCells=newRowNumbers-rowNumbers
如果newCells>0{
debugPrint(“ae.ui添加单元格\(新单元格)”)
设rowInd=RowNumber-1
var IndexPath=[IndexPath]()
因为我在1…新的牢房里{
append(IndexPath(行:rowInd+i,节:0))
}
self.beginUpdate()
self.insertRows(位于:indexPaths,带:。无)
self.endUpdates()
}
}
}

我也遇到了同样的问题-它崩溃了,因为代码在willDisplayCell中。感谢@A-live的评论

在scrollViewDidScroll中移动代码后,其工作正常,类似于:

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let offsetY = scrollView.contentOffset.y
    let contentHeght = scrollView.contentSize.height
    let scrollViewHeight = scrollView.frame.size.height

    if (contentHeght - offsetY) < 3*scrollViewHeight {
        //we are less than 3 page screens from the bottom

        let rowNumbers = self.dataModel.getVisibleCells()
        let newRowNumbers = self.dataModel.getMoreCells()
        let newCells = newRowNumbers - rowNumbers

        if newCells > 0 {
            debugPrint("ae.ui adding cells \(newCells)")

            let rowInd = rowNumbers - 1
            var indexPaths = [IndexPath]()
            for i in 1...newCells {
                indexPaths.append(IndexPath(row: rowInd+i, section: 0))
            }

            self.beginUpdates()
            self.insertRows(at: indexPaths, with: .none)
            self.endUpdates()
        }
    }
}
func scrollViewDidScroll(scrollView:UIScrollView){
let offsetY=scrollView.contentOffset.y
让ContentHeight=scrollView.contentSize.height
设scrollViewHeight=scrollView.frame.size.height
if(contentheight-offsetY)<3*滚动视图高度{
//我们从底部开始的屏幕不到3页
让rowNumbers=self.dataModel.getVisibleCells()
让newRowNumbers=self.dataModel.getMoreCells()
设newCells=newRowNumbers-rowNumbers
如果newCells>0{
debugPrint(“ae.ui添加单元格\(新单元格)”)
设rowInd=RowNumber-1
var IndexPath=[IndexPath]()
因为我在1…新的牢房里{
append(IndexPath(行:rowInd+i,节:0))
}
self.beginUpdate()
self.insertRows(位于:indexPaths,带:。无)
self.endUpdates()
}
}
}

尝试使用
让beginIndex=cellCount-1
?也许你是在最后一个细胞之后插入我想是在最后一个细胞之后插入新细胞的想法。问题是,插入是在设计用于修改与其相关的单个单元格的方法上实现的,在这一点上,不能保证整个表视图正确配置并为插入新行做好准备,因此这种操作是不安全的,即使它能工作或曾经工作过。也许值得尝试调试并找出什么是
[0..9]
嗨,谢谢你的评论@A-Live,是的,我们的想法是在最后一个单元格之后插入新的单元格。如果我正确理解了您的评论,那么此时操作是不安全的,因为它是在“willDisplayCell”方法中实现的,对吗?您是否有其他方法可以通过安全操作获得相同的结果?您可以使用滚动视图委托:如果a)当前显示的单元格最初没有占据可用高度,或b)达到约90%滚动偏移的阈值,则加载下一个“页面”。我不能肯定这会完美地工作-这个问题可能与自动布局引擎有关。既然您已经有了一个可以重现问题的小样本项目,我建议您创建一个Apple请求并附上该样本项目,说明您认为这是Apple的问题,并请求帮助寻找解决方法。请尝试使用
let beginIndex=cellCount-1
?也许你是在上一次之后插入的我想这是插入的想法