@IBOutlet weak var mainTableView: UITableView!
var taskArray: [Task]? = nil
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "mainCell") as! mainCell
taskArray = CoreDataHandler.fetchObject()
let task = taskArray![indexPath.row]
cell.titleLabel.text = task.title
//cell.dateLabel.text = String(task.date)
return cell
}
我总是得到这样的错误:“Thread1: Fatal error: Index out out range”:let task = taskArray。有人能帮我吗?
发布于 2018-06-09 20:55:32
您必须在此处发送数组计数,不要返回静态数字,因为它可能会超过数组计数
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return taskArray.count
}
//同时转换
var taskArray = [Task]()
//
抓取应该在viewDidLoad
中
override func viewDidLoad() {
super.viewDidLoad()
taskArray = CoreDataHandler.fetchObject()
}
不在cellForRowAt
内部
https://stackoverflow.com/questions/50774506
复制相似问题