Ios 如何避免重新加载整个表,从而中断滚动

Ios 如何避免重新加载整个表,从而中断滚动,ios,swift,uitableview,Ios,Swift,Uitableview,我想把元素的数量限制为三个。当我添加元素4时,元素1应该消失 如果我想继续正常滚动,我应该通过调用deleteRowsAtIndexPaths告诉UITableView我正在删除整个部分中的行。之后,我需要告诉UITableView,您正在第三个节(节索引2)插入一组行 这样可以避免重新加载整个表,从而中断滚动 但不幸的是,这对我不起作用 告诉我这个错误: 'attempt to delete row 3 from section 2, but there are only 1 sections

我想把元素的数量限制为三个。当我添加元素4时,元素1应该消失

如果我想继续正常滚动,我应该通过调用
deleteRowsAtIndexPaths
告诉
UITableView
我正在删除整个
部分中的行。之后,我需要告诉
UITableView
,您正在第三个
(节索引2)插入一组行

这样可以避免重新加载整个表,从而中断滚动

但不幸的是,这对我不起作用

告诉我这个错误:

'attempt to delete row 3 from section 2, but there are only 1 sections before the update'
代码:

    //scrolling down time calling this method
    func oldestState(){
        //var students : [[Student]]? = [[],[],[]]
        let data = getdata()
        self.students?[(self.firstIndex+self.count) % (self.students?.count)!] = data
        if (self.count != 3) {
            self.count += 1
        } else {
            self.firstIndex = (self.firstIndex+1) % (self.students?.count)!
        }

        self.newsFeedTableView.beginUpdates()
        let indexPathsDeleted = (0..<(data
            .count)).map { IndexPath(row: $0, section: (self.students?.count)! - 1) }
        self.newsFeedTableView.deleteSections([0], with: UITableViewRowAnimation.automatic)
        self.newsFeedTableView.deleteRows(at: indexPathsDeleted, with: UITableViewRowAnimation.automatic)
        self.newsFeedTableView.endUpdates()

        self.newsFeedTableView.beginUpdates()
        let indexPathsInsert = (0..<(data
.count)).map { IndexPath(row: $0, section: (self.students?.count)! - 1) }
        self.newsFeedTableView.insertSections([2], with: UITableViewRowAnimation.automatic)
        self.newsFeedTableView.insertRows(at: indexPathsInsert, with: UITableViewRowAnimation.automatic)
        self.newsFeedTableView.endUpdates()
    }


    func getdata() -> [Student]{

        var _students = [Student]()
        for i in  itemNumber..<(itemNumber + 4) {
            let student = Student()
            student.name = "\(i)"
             print("adding student roll number : \(student.name)")
            _students.append(student)
        }
        itemNumber  +=  4
        return _students
    }
}
//向下滚动调用此方法的时间
func oldestState(){
//变量学生:[[Student]]?=[],[],[]
let data=getdata()
self.students?[(self.firstIndex+self.count)%(self.students?.count)!]=数据
如果(self.count!=3){
self.count+=1
}否则{
self.firstIndex=(self.firstIndex+1)%(self.students?.count)!
}
self.newsFeedTableView.beginUpdate()

让indexPathsDeleted=(0..我不知道tableview的数据源的行为,但是从错误中,它清楚地告诉我们,您只有一个节,因此您不能处理节2,因为没有节2。您只能处理节0。 您可以删除整行,并在节0中添加行。此外,您应该处理self.students数组,让它与tableview行匹配,否则它将崩溃

更新:

此提交解决了表格滚动的问题

此提交解决了表格滚动变得急促的问题


完整的源代码,请您检查一下好吗?我已经阅读了代码,只有on部分,第一次调用initUpdateUI,count属性是1。因此,该部分是1。您能帮我解决这个问题吗?我该怎么办?我最多花10天时间。请看啊,我可以帮您,但我想知道您想要什么,该部分有3行第0节和srcoll,你想要什么?初始提要将下载4篇文章供预览,但如果用户向下滚动,则加载更多文章,因此我需要将节添加到底部表格视图,并从顶部删除节。但tableview最多保留3节。第一次下载后加载节1。第二次下载并加载节2.第3次下载加载第3节。现在第4次下载加载第3节,并从第1节中删除第1节。如果最多只有3行,则只需重新加载整个表。这样代码更容易理解,而且您不太可能需要对其进行优化。@n第4次我需要最多加载3节,每个节都包含基于api响应的行item@NRitH每个请求返回20个post或更多,它将能够附加一个节,因此当用户滚动并转到节的中间时,用户将调用web请求并插入一个节。当节达到3,然后4时,请求将插入节并从顶部部分删除到表视图。这有意义吗?您会这样做吗请告诉我