var data = [InboxPosts]()
func setup() {
AF.request("https://example.com", method: .get, parameters: parameters, headers: headers)
.validate().responseData { response in
switch response.result {
case .success(let data):
//get data from service
let rel = InboxPosts(userid: id, name: name)
if !self.data.contains(rel) {
self.data.append(rel)
}
let indexSet = IndexSet(integer: self.data.count - 1)
self.tableView.beginUpdates()
self.tableView.insertSections(indexSet, with: .automatic)
self.tableView.endUpdates()
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
setup()
}
I add data to array and call InsertSections
func tableView(_: UITableView, numberOfRowsInSection _: Int) -> Int {
return 1
}
func numberOfSections(in _: UITableView) -> Int {
return data.count
}
If I use reloadData() it works good but when I use insertSections I get
Invalid update: invalid number of sections. The number of sections contained in the table view after the update (16) must be equal to the number of sections contained in the table view before the update (0), plus or minus the number of sections inserted or deleted (1 inserted, 0 deleted)
When the new data is available I don't want to use reloadData and add cells using insertSections but how can I fix this issue?
CodePudding user response:
I fixed it by using this code
self.tableView.beginUpdates()
self.tableView.insertSections(NSIndexSet(indexesIn: NSMakeRange(0, self.data.count)) as IndexSet, with: .automatic)
self.tableView.endUpdates()