我让tableView在一段时间内填充,但随后对我如何从Parse获取数据进行了一些调整,从那以后我无法显示我的tableView而不会因致命错误而崩溃:数组索引超出范围 这是我的代码:
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
}
}
这一行因致命错误而崩溃:
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"]
但同样的交易,只是在[&#34; name&#34;]行崩溃了。我究竟做错了什么?非常感谢任何帮助!
答案 0 :(得分:0)
对我来说非常好,没有任何错误或警告! 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
}
}
模拟器上运行的代码:
答案 1 :(得分:0)
这里的解决方法使代码更简单,效果更好!
var sections = ["Core Dog Vaccines", "Non-Core Dog Vaccines"]
var titles = [["Rabies 1-yr", "Rabies 3-yr", "Distemper", "Parvovirus", "Adenovirus"], ["Parainfluenza", "Bordetella", "Lyme Disease", "Leptospirosis", "Canine Influenza"]]
var details = [["No Dogument", "No Dogument", "No Dogument", "No Dogument", "No Dogument"], ["No Dogument", "No Dogument", "No Dogument", "No Dogument", "No Dogument"]]
var textCellIdenifier = "vaccineCell"
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdenifier, forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = self.titles[indexPath.section][indexPath.row]
cell.detailTextLabel?.text = self.details[indexPath.section][indexPath.row]
return cell
}
而不是一个复杂的数组&lt;阵列&LT;字典&LT;字符串,字符串&GT;&GT;&GT;我只是为细节制作了一个单独的阵列。呃让它变得复杂,开始时很糟糕,但仍然每天都在学习!现在我可以从解析中加载数据,如下所示:
let rabies1Query = PFQuery(className: "Vaccine")
rabies1Query.whereKey("userId", equalTo: (PFUser.currentUser()?.objectId)!)
rabies1Query.whereKey("newID", equalTo: self.dogObject)
rabies1Query.whereKey("vaccineType", equalTo: "Rabies 1-yr")
rabies1Query.findObjectsInBackgroundWithBlock({ (object, error) -> Void in
if error == nil {
if let object = object {
for object in object {
if object["expired"] as! Bool == true {
self.details[0][0] = self.expired
print("printing rabies 1-yr - EXPIRED")
} else if object["expired"] as! Bool == false {
self.details[0][0] = self.upToDate
print("printing rabies 1-yr - UP TO DATE")
}
}
}
} else {
print("printing rabies 1-yr - No Document")
}
})
等等,每个标题都会更改详细信息数组位置的位置。希望别人能看到并学到一些东西!