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()
}
我將資料添加到陣列并呼叫 InsertSections
func tableView(_: UITableView, numberOfRowsInSection _: Int) -> Int {
return 1
}
func numberOfSections(in _: UITableView) -> Int {
return data.count
}
如果我使用 reloadData() 它作業得很好但是當我使用 insertSections 我得到
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?
uj5u.com熱心網友回復:
我使用此代碼修復了它
self.tableView.beginUpdates()
self.tableView.insertSections(NSIndexSet(indexesIn: NSMakeRange(0, self.data.count)) as IndexSet, with: .automatic)
self.tableView.endUpdates()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/456683.html
標籤:ios swift uitableview
上一篇:是否可以訪問擁抱面變壓器嵌入層?
