Reputation: 10590
I wanted to cap the number of elements to three. When I add element four, element one should disappear.
If I want scrolling to continue fine, I should tell UITableView
that I am deleting rows in the entire section
zero by calling deleteRowsAtIndexPaths
. After that I need to tell UITableView
that you are inserting a bunch of rows at the third section
(section index 2).
This way you would be able to avoid reloading the whole table, disrupting the scroll.
But unfortunately it is not working for me.
Give me this error :
'attempt to delete row 3 from section 2, but there are only 1 sections before the update'
code :
//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
}
}
Upvotes: 1
Views: 144
Reputation: 84
I don't know the dataSource of the tableview behave,but from the error,it tells us clearly,that you have only one section,so you can't handle the section 2,because there isn't section 2.You can only handle the section 0. You can delete the whole rows,and add row in section 0.besides,You should handle the self.students array,let it match the tableview rows,or it will crash.
UPDATE:
This commit resolve the issue Table scrolling
I have correct it,This version.
This commit resolve the issue Table scrolling get jerky
I have correct it,This version.
Upvotes: 2