我试图在TableView中的2个不同部分划分2个NSObject数组,但我在xCode中看到错误'无法下标类型为[[DataMode]]的值。
我的DataModel是:
class dataModel: NSObject {
var name:String
var val:Double
var prot:Double
var fat:Double
var cal:Double
var grams:Double
var total:Double
init(name: String, val: Double,prot: Double, fat: Double, cal:Double, grams: Double, total: Double){
self.name = name
self.val = val
self.prot = prot
self.fat = fat
self.cal = cal
self.grams = grams
self.total = total
}
}
阵列:
var MeatData = [dataModel(name: "food1", val: 5.0, prot: 17.5, fat: 11.7, cal:178.0, grams: 0.0, total: 0.0),
dataModel(name: "meat2", val: 6.0, prot: 19.0, fat: 7.8, cal: 148, grams: 0.0, total: 0.0),
dataModel(name: "meat3", val: 7.0, prot: 24.5, fat: 29.7, cal: 370.0, grams: 0.0, total: 0.0)]
var BreadData = [dataModel(name: "bread1", val: 5.0, prot: 17.5, fat: 11.7, cal: 178.0, grams: 0.0, total: 0.0),
dataModel(name: "bread2", val: 6.0, prot: 19.0, fat: 7.8, cal: 148, grams: 0.0, total: 0.0),
dataModel(name: "bread3", val: 7.0, prot: 24.5, fat: 29.7, cal: 370.0, grams: 0.0, total: 0.0)]
TableViewController:
导入UIKit
class MeatTableViewController: UITableViewController {
let sections = ["Meat","Bread"]
let items = [[MeatData], [BreadData]]
@IBOutlet weak var addBarButton: UIBarButtonItem!
var stringToPass = ""
var valueToPass:Double!
var protToPass:Double!
var fatToPass:Double!
var calToPass:Double!
//vars additional food
var NewFood = ""
var NewCarbs:Double!
var NewProt:Double!
var NewFat:Double!
var NewCal:Double!
var NewlyAddedFood = dataModel(name: "", val: 0.0, prot: 0.0, fat: 0.0, cal: 0.0, grams: 0.0, total: 0.0)
@IBOutlet var meatTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sections[section]
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return sections.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return items[section].count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//let cell = tableView.dequeueReusableCellWithIdentifier("meatCell", forIndexPath: indexPath) as! MeatTableViewCell
let cellIdentifier = "meatCell"
let Meatcell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MeatTableViewCell
let meatIndex = MeatData[indexPath.row]
Meatcell.titleLabel?.text = items[indexPath.section][indexPath.row]
//Meatcell.titleLabel.text = meatIndex.name
//Meatcell.valLabel.text = String(meatIndex.val)
//Meatcell.protEMPTY.text = String(meatIndex.prot)
//Meatcell.fatEMPTY.text = String(meatIndex.fat)
//Meatcell.calEMPTY.text = String(meatIndex.cal)
return Meatcell
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
MeatData.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("You selected cell #\(indexPath.row)!")
let indexPathVal: NSIndexPath = tableView.indexPathForSelectedRow!
print("\(indexPathVal)")
let currentCell = tableView.cellForRowAtIndexPath(indexPathVal) as! MeatTableViewCell;
print("\(currentCell)")
print("\(currentCell.titleLabel?.text!)")
stringToPass = currentCell.titleLabel.text!
valueToPass = Double(currentCell.valLabel.text!)
protToPass = Double(currentCell.protEMPTY.text!)
fatToPass = Double(currentCell.fatEMPTY.text!)
calToPass = Double(currentCell.calEMPTY.text!)
print(stringToPass)
print(valueToPass)
print(protToPass)
print(fatToPass)
print(calToPass)
self.performSegueWithIdentifier("showCalc", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//Here i am checking the Segue and Saving the data to an array on the next view Controller also sending it to the next view COntroller
if segue.identifier == "showCalc"{
//Creating an object of the second View controller
let DestController = segue.destinationViewController as! CalcViewController
//Sending the data here
DestController.passedString = stringToPass
DestController.passedValue = valueToPass
DestController.passedProt = protToPass
DestController.passedFat = fatToPass
DestController.passedCal = calToPass
}
}
@IBAction func done(segue:UIStoryboardSegue) {
let carDetailVC = segue.sourceViewController as! AdditionalFood
NewFood = String(carDetailVC.AddedFood)
NewCarbs = carDetailVC.AddedCarbs
NewProt = carDetailVC.AddedProteins
NewFat = carDetailVC.AddedFat
NewCal = carDetailVC.AddedCal
NewlyAddedFood.name = NewFood
NewlyAddedFood.val = NewCarbs
NewlyAddedFood.prot = NewProt
NewlyAddedFood.fat = NewFat
NewlyAddedFood.cal = NewCal
MeatData.insert(NewlyAddedFood, atIndex: 0)
self.tableView.reloadData()
}
}
结果我发现了一条错误消息
'不能在这里下标[[DataMode]]类型的值: Meatcell.titleLabel?.text = items [indexPath.section] [indexPath.row]