我试着用这个答案Cant insert rows into UITableView - Swift 3向表视图插入行,它工作得很好,但有问题,他们显示,但跳过一些数据。
基本上我的问题是我有2D数组var students : [[Student]]?,我想插入一个有4行的部分。
在表视图中显示插入行的正确方式是什么。
到目前为止,我所尝试的
func numberOfSections(in tableView: UITableView) -> Int {
return students?.count ?? 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return students?[section].count ?? 0
}并插入:
//scrolling down time calling this method
func oldestState(){
self.students?.append(getdata()) //Update students property
self.newsFeedTableView.beginUpdates()
let indexPaths = (0..<(getdata().count)).map { IndexPath(row: $0, section: (self.students?.count)! - 1) }
print(indexPaths)
self.newsFeedTableView.insertSections([(self.students?.count)! - 1], with: UITableViewRowAnimation.automatic)
self.newsFeedTableView.insertRows(at: indexPaths, 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
}这是输出结果。如果您看到日志,您将看到在表视图中跳过第8-11行

发布于 2017-08-03 18:59:48
我希望这个问题是因为getdata()方法被调用了两次,因此itemNumber值增加了4,两次
试试这个:
let data = getdata()
self.students?.append(data) //Update students property
self.newsFeedTableView.beginUpdates()
let indexPaths = (0..<(data.count)).map { IndexPath(row: $0, section: (self.students?.count)! - 1) }https://stackoverflow.com/questions/45480244
复制相似问题