简体   繁体   English

需要帮助设置数据模型以显示tableView

[英]Need help setting up data model to display tableView

I had my tableView populating as it should for a little while but then made some tweaks to how I get my data from Parse and since then I can't get my tableView to display without crashing with fatal error: Array index out of range Here is my code: 我将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
}
}

This line crashes with the fatal error: 此行崩溃并出现致命错误:

let object = vaccineEntry[indexPath.section][indexPath.row]

Or I can not make an object variable and just go: 或我不能使对象变量而只是去:

cell.textLabel?.text = vaccineEntry[indexPath.section][indexPath.row]["name"]
cell.textLabel?.text = vaccineEntry[indexPath.section][indexPath.row]["detail"]

But same deal, just crashes on the ["name"] line. 但是同样的事情,只是在[“ name”]行上崩溃了。 What am I doing wrong? 我究竟做错了什么? Any help is much appreciated! 任何帮助深表感谢!

Works perfectly fine for me without any errors or warnings! 对我来说工作正常,没有任何错误或警告! PFB the code: 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
}

}

The running code on simulator: 在模拟器上运行的代码:

你的看法

Here's the work-around that made the code simpler and work better! 这是使代码更简单,更好地工作的变通方法!

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
}

Instead of having a complicated Array< Array< Dictionary< String,String>>> I just made a separate array for the details.. Ugh making it complicated to begin with sucks but still learning everyday! 我没有制作复杂的Array <Array <Dictionary <字典<String,String >>>,而是为细节制作了一个单独的数组。 Now I can load data from parse like so: 现在,我可以像这样从解析中加载数据:

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")
        }
    })

And so on for each title changing the location of the details array location. 依此类推,为每个标题更改详细信息数组位置的位置。 Hope someone else can see and learn something! 希望其他人可以看到和学习​​一些东西!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM