【发布时间】:2018-01-04 07:41:47
【问题描述】:
当滚动tableView 时更改segmentedControl 索引时,我在cellForRowAt 中得到“Index out of range”。
@IBAction func segmentedControlAction(_ sender: AnyObject!) {
if(segmentedControl.selectedSegmentIndex == 0) {
self.data?.removeAll()
loadA()
}
else if(segmentedControl.selectedSegmentIndex == 1){
self.data?.removeAll()
loadB()
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! CustomCell
cell.data = self.data?[indexPath.row] ---> Error here
return cell
}
LoadA 和 LoadB 是类似的函数
func loadA(){
API.sharedInstance.loadA(start, callback: { response in
if let data = response["data"].arrayValue as [JSON]?{
self.data = data
DispatchQueue.main.async {
self.tableView?.reloadData()
}
}
})
}
【问题讨论】:
-
显然你正在重置
self.data?.removeAll()并通过索引访问空对象。 -
更新问题
标签: swift scroll tableview uisegmentedcontrol