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

标签 ios swift uitableview

我让我的 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
}

}

模拟器上运行的代码:

your view

关于ios - 需要帮助设置数据模型以显示 tableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34343327/

相关文章:

ios - 类型 '_' 的值没有成员

ios - 准备在按下按钮时 segue 不发送信息

ios - 我正在尝试创建一个可扩展的 TableView ,其子元素具有 UICollectionView

ios - ipad-如何在 UITableViewCell 中打开弹出 View Controller

ios - 如何使用 objective-c 查看 PDF 文件中的数字签名

ios - 如何使用 objective-c 在ios中使用Web服务URL实现搜索选项

swift - 下面描述的方法可以在什么情况下使用?谁能提供一些有用的例子?

ios - TableView 单元格再次显示

ios - tableviewCell的字幕样式添加UISwitch

ios - 在项目中使用 WYPopover 时 UINavigationControllerDelegate 方法调用崩溃