Ios Swift 3中的索引超出范围
我总是在这一行得到错误:“线程1:致命错误:索引超出范围”:let task=taskArray![indexath.row]。有人能帮我吗 您必须在此处发送数组计数,不要返回静态数字,因为它可能会超过数组计数Ios Swift 3中的索引超出范围,ios,swift,uitableview,Ios,Swift,Uitableview,我总是在这一行得到错误:“线程1:致命错误:索引超出范围”:let task=taskArray![indexath.row]。有人能帮我吗 您必须在此处发送数组计数,不要返回静态数字,因为它可能会超过数组计数 @IBOutlet weak var mainTableView: UITableView! var taskArray: [Task]? = nil func tableView(_ tableView: UITableView, numberOfRowsInSection sect
@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
}
//也转换
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return taskArray.count
}
//
取数应在viewDidLoad
var taskArray = [Task]()
请不要在
cellForRowAt
内张贴有如此多空白的代码
override func viewDidLoad() {
super.viewDidLoad()
taskArray = CoreDataHandler.fetchObject()
}