首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Swift 3中的索引超出范围

Swift 3中的索引超出范围
EN

Stack Overflow用户
提问于 2018-06-09 20:54:39
回答 1查看 112关注 0票数 2
@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。有人能帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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内部

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50774506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档