TableView indexPath致命错误:索引超出范围

时间:2017-08-16 08:24:19

标签: swift uitableview

我有以下代码来填充单元格,当我告诉底部它给出致命错误。我观察到它可能是由于数组中的单元格捕获nil值。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as UITableViewCell!

    // Set text from the data model
    cell.textLabel?.text = values[indexPath.row]
    cell.textLabel?.font = textField.font

    return cell
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return values.count;
}

1 个答案:

答案 0 :(得分:0)

如果只使用一个部分,请在此处使用此功能。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return values.count
}

多个部分的类似解决方法。只需检查您是否永远不会访问超过values的{​​{1}}值。

相关问题