我让我的 tableView 按应有的方式填充了一段时间,但后来对如何从 Parse 获取数据进行了一些调整,从那时起我就无法让我的 tableView 显示而不崩溃 fatal error :数组索引超出范围 这是我的代码:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var vaccineEntry: Array<Array<Dictionary<String,String>>> = [[
["name" : "Rabies 1-yr", "detail": "None"],
["name" : "Rabies 3-yr", "detail": "None"],
["name" : "Distemper", "detail": "None"],
["name" : "Parvovirus", "detail": "None"],
["name" : "Adenovirus", "detail": "None"]],
[
["name" : "Parainfluenza", "detail": "None"],
["name" : "Bordetella", "detail": "None"],
["name" : "Lyme Disease", "detail": "None"],
["name" : "Leptospirosis", "detail": "None"],
["name" : "Canine Influenza", "detail": "None"]
]]
var sections = ["Core Vaccines", "Non-Core Vaccines"]
var titles = [["Rabies 1-yr", "Rabies 3-yr", "Distemper", "Parvovirus", "Adenovirus"], ["Parainfluenza", "Bordetella", "Lyme Disease", "Leptospirosis", "Canine Influenza"]]
var textCellIdenifier = "vaccineCell"
// Section Headers
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sections[section]
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.sections.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.titles[section].count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdenifier, forIndexPath: indexPath) as UITableViewCell
let object = vaccineEntry[indexPath.section][indexPath.row]
cell.textLabel?.text = object["name"]
cell.detailTextLabel?.text = object["detail"]
return cell
}
}
此行因 fatal error 而崩溃:
let object = vaccineEntry[indexPath.section][indexPath.row]
或者我不能创建一个对象变量,然后就可以了:
cell.textLabel?.text = vaccineEntry[indexPath.section][indexPath.row]["name"]
cell.textLabel?.text = vaccineEntry[indexPath.section][indexPath.row]["detail"]
但是同样的交易,只是在 ["name"] 行崩溃。我究竟做错了什么?非常感谢任何帮助!
最佳答案
对我来说工作得很好,没有任何错误或警告! PFB代码:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var vaccineEntry: Array<Array<Dictionary<String,String>>> = [[
["name" : "Rabies 1-yr", "detail": "None"],
["name" : "Rabies 3-yr", "detail": "None"],
["name" : "Distemper", "detail": "None"],
["name" : "Parvovirus", "detail": "None"],
["name" : "Adenovirus", "detail": "None"]],
[
["name" : "Parainfluenza", "detail": "None"],
["name" : "Bordetella", "detail": "None"],
["name" : "Lyme Disease", "detail": "None"],
["name" : "Leptospirosis", "detail": "None"],
["name" : "Canine Influenza", "detail": "None"]
]]
var sections = ["Core Vaccines", "Non-Core Vaccines"]
var titles = [["Rabies 1-yr", "Rabies 3-yr", "Distemper", "Parvovirus", "Adenovirus"], ["Parainfluenza", "Bordetella", "Lyme Disease", "Leptospirosis", "Canine Influenza"]]
var textCellIdenifier = "vaccineCell"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: self.textCellIdenifier)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sections[section]
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.sections.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.titles[section].count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdenifier, forIndexPath: indexPath) as UITableViewCell
let object = vaccineEntry[indexPath.section][indexPath.row]
cell.textLabel?.text = object["name"]
cell.detailTextLabel?.text = object["detail"]
return cell
}
}
模拟器上运行的代码:
关于ios - 需要帮助设置数据模型以显示 tableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34343327/