import UIKit
class ServicesTableViewController: UITableViewController {
var services = [Service]()
override func viewDidLoad() {
super.viewDidLoad()
//load services data
loadServices()
}
private func loadServices(){
let photo1 = UIImage(named: "blank")
let photo2 = UIImage(named: "blank1")
let photo3 = UIImage(named: "blank2")
guard let Service1 = Service(titled: "blank", description:"Test", imageName: photo1) else {
fatalError("Unable to instantiate Service1")
}
guard let Service2 = Service(titled: "blank1", description:"Test", imageName: photo2) else {
fatalError("Unable to instantiate Service1")
}
guard let Service3 = Service(titled: "blank2", description:"Test", imageName: photo3) else {
fatalError("Unable to instantiate Service1")
}
services += [Service1,Service2,Service3]
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return services.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Table view cells are reused and should be dequeued using a cell identifier.
let cellIdentifier = "ServicesTableViewCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? ServicesTableViewCell else {
fatalError("The dequeued cell is not an instance of ServicesTableViewCell.")
}
// Fetches the appropriate service for the data source layout.
let services = Service[indexPath.row]
cell.titleLabel.text = service.titled
cell.photoImageView.image = service.image
cell.descriptionLabel.text = service.description
return cell
}
我一直收到错误Service.type没有下标成员。我试图将这些数据输入到表视图控制器中,这是表视图控制器内部的代码,如果需要,我也可以发布单元代码和service.swift文件。
答案 0 :(得分:3)
你应该改变这个:
let services = Service[indexPath.row] // this is a class, not an array
in
let service = services[indexPath.row]
我也看到你初始化Service
传递了一个名为imageName
UIImage
类型的参数,所以它可能会让你感到困惑,我想你的问题可能会有问题cellForRowAt
1}},因为:
cell.photoImageView.image = service.image
因此,请确保您已在Service
中声明了此类属性(image!= imageName)。