Swift 从自定义表视图添加单元格时出错

Swift 从自定义表视图添加单元格时出错,swift,multidimensional-array,Swift,Multidimensional Array,这是我的TableViewController,它应该将单元格添加到屏幕上 import UIKit class categoryTableViewController: UITableViewController { var selectedItem = "" var selectedIndex: Int = 0 var items = [[dataClass]]() //Double array of the shopping item Make new swift for data

这是我的TableViewController,它应该将单元格添加到屏幕上

import UIKit

class categoryTableViewController: UITableViewController {
var selectedItem = ""
var selectedIndex: Int = 0


var items = [[dataClass]]()

//Double array of the shopping item Make new swift for data classes
override func viewDidLoad() {
    super.viewDidLoad()
    items=[
        [dataClass(image: UIImage(named: "grocery-1-tomatoes")!, header: "Tomatoes", price:5, description:"lsdfjsdnfsjdf"),
         dataClass(image: UIImage(named: "grocery-2-bananas")!, header: "bananas", price:5, description:"lsdfjsdnfsjdf"),
         dataClass(image: UIImage(named: "grocery-3-gala")!, header: "gala", price:5, description:"lajsdljasd"),
         dataClass(image: UIImage(named: "grocery-4-lettuce")!, header: "lettuce", price:5, description:"lajsdljasd"),
         dataClass(image: UIImage(named: "grocery-5-broccoli")!, header: "broccoli", price:5, description:"lajsdljasd"),
         dataClass(image: UIImage(named: "grocery-6-milk")!, header: "milk", price:5, description:"lajsdljasd"),
         dataClass(image: UIImage(named: "grocery-7-bread")!, header: "bread", price:5, description:"lajsdljasd"),
         dataClass(image: UIImage(named: "grocery-8-eggs")!, header: "eggs", price:5, description:"lajsdljasd")
        ],

        [dataClass(image: UIImage(named: "garden-1-shovel")!, header: "shovel", price:53, description:"lsdfjsdnfsjdf"),
         dataClass(image: UIImage(named: "garden-2-tomato-plant")!, header: "Tomato Plant", price:5, description:"lsdfjsdnfsjdf"),
         dataClass(image: UIImage(named: "garden-3-mower")!, header: "mower", price:5, description:"lsdfjsdnfsjdf")
        ]
    ]
    tableView.dataSource = self
    tableView.delegate = self
    print(selectedItem)
    print(selectedIndex)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows

    return items[selectedIndex].count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "categoryView", for: indexPath) as! customTableViewCell

    cell.categoryImageView = UIImageView(image: items[selectedIndex][indexPath.row].categoryImageView) 
 //right here I get the error type dataClass has no subscript members
    cell.categoryNameLabel = UILabel(header: items[selectedIndex][indexPath.row].categoryLabel)
//right here I get the error value of type 'inout [[dataClass]]' (aka 'inout Array <array<dataClass>>')
    cell.categoryDescriptionLabel = UILabel(description: items[selectedIndex][indexPath.row].categoryDescription)
//right here I get the error value of type 'inout [[dataClass]]' (aka 'inout Array <array<dataClass>>')

    return cell

}
}
这是我的数据类 导入UIKit

class customTableViewCell: UITableViewCell {

@IBOutlet weak var categoryPriceLabel: UILabel!
@IBOutlet weak var categoryImageView: UIImageView?
@IBOutlet weak var categoryDescriptionLabel: UILabel!
@IBOutlet weak var categoryNameLabel: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    // Configure the view for the selected state
}

}
class dataClass{

var categoryImage: UIImage
var categoryLabel: String
var categoryPrice: Int
var categoryDescription: String
init(image: UIImage, header: String, price:Int, description:String){
    self.categoryImage = image
    self.categoryLabel = header
    self.categoryPrice = price
    self.categoryDescription = description
}
}
我试图以编程方式将不同的类别添加到页面中,但我不知道如何修复这些错误

更换这个

cell.categoryImageView = UIImageView(image: items[selectedIndex][indexPath.row].categoryImageView) 
//right here I get the error type dataClass has no subscript members
cell.categoryNameLabel = UILabel(header: items[selectedIndex][indexPath.row].categoryLabel)
//right here I get the error value of type 'inout [[dataClass]]' (aka 'inout Array <array<dataClass>>')
cell.categoryDescriptionLabel = UILabel(description: items[selectedIndex][indexPath.row].categoryDescription)
//right here I get the error value of type 'inout [[dataClass]]' (aka 'inout Array <array<dataClass>>')

您可以按照@Sahil所述修复这些错误,或者:

修复您的
customTableViewCell
 

和替换:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "categoryView", for: indexPath) as! customTableViewCell

    cell.categoryImageView = UIImageView(image: items[selectedIndex][indexPath.row].categoryImageView) 
 //right here I get the error type dataClass has no subscript members
    cell.categoryNameLabel = UILabel(header: items[selectedIndex][indexPath.row].categoryLabel)
//right here I get the error value of type 'inout [[dataClass]]' (aka 'inout Array <array<dataClass>>')
    cell.categoryDescriptionLabel = UILabel(description: items[selectedIndex][indexPath.row].categoryDescription)
//right here I get the error value of type 'inout [[dataClass]]' (aka 'inout Array <array<dataClass>>')

    return cell

}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "categoryView", for: indexPath) as! customTableViewCell

    cell.data = items[selectedIndex][indexPath.row]

    return cell

} 

错误在哪里?
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "categoryView", for: indexPath) as! customTableViewCell

    cell.categoryImageView = UIImageView(image: items[selectedIndex][indexPath.row].categoryImageView) 
 //right here I get the error type dataClass has no subscript members
    cell.categoryNameLabel = UILabel(header: items[selectedIndex][indexPath.row].categoryLabel)
//right here I get the error value of type 'inout [[dataClass]]' (aka 'inout Array <array<dataClass>>')
    cell.categoryDescriptionLabel = UILabel(description: items[selectedIndex][indexPath.row].categoryDescription)
//right here I get the error value of type 'inout [[dataClass]]' (aka 'inout Array <array<dataClass>>')

    return cell

}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "categoryView", for: indexPath) as! customTableViewCell

    cell.data = items[selectedIndex][indexPath.row]

    return cell

}