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

时间:2017-03-03 06:32:29

标签: 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 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

}
}

这是我的customTableViewCell类     导入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
}

}

这是我的dataClass     导入UIKit

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

我试图以编程方式在页面中添加不同的类别,但我不知道如何修复这些错误。

2 个答案:

答案 0 :(得分:1)

替换此

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>>')

使用

cell.categoryImageView.image = items[selectedIndex][indexPath.row].categoryImage

cell.categoryNameLabel.text = items[selectedIndex][indexPath.row].categoryLabel

cell.categoryDescriptionLabel.text = items[selectedIndex][indexPath.row].categoryDescription

答案 1 :(得分:0)

您可以将这些错误修复为@Sahil提到或:

修复您的 customTableViewCell    

   import UIKit

    class customTableViewCell: UITableViewCell {

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

        // -- declare new variable --
        var data: dataClass? {
            didSet {
                self.categoryImageView.image = data. categoryImage
                self.categoryNameLabel.text = data.categoryLabel
                self.categoryDescriptionLabel.text = data.categoryDescription
            }
        }
        // -------------------

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

并替换:

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

}