Я пытаюсь создать базовое приложение Swift для отображения автомобилей. У меня есть настройка UITableView. В настоящее время я определил две машины, но по какой-то причине отображается только 1.Ячейки UITableView отображаются только 1 ячейка
Model.swift
import Foundation
import UIKit
public enum CarColor {
case White
case Blue
case Black
case Red
}
class Model {
var title: String
var description: String
var image: UIImage
var color: CarColor
init(titled: String, description: String, imageName: String){
self.title = titled
self.description = description
if let img = UIImage(named:imageName){
image = img
} else {
image = UIImage(named:"default")!
}
color = .White
}
}
ModelLine.swift
import Foundation
class ModelLine {
var name: String
var models: [Model]
init(named: String, includeModels: [Model]){
name = named
models = includeModels
}
class func modelLines() -> [ModelLine]{
return[self.hondaCRV(), self.hondaPilot()]
}
private class func hondaCRV() -> ModelLine {
var models = [Model]()
models.append(Model(titled:"2016 Honda CR-V", description:"All day and into the night, CR-V delivers dynamic crossover performance and style. It's also incredibly spacious, with luxury amenities at the top of its class.", imageName: "crv"))
return ModelLine(named:"Honda CR-V", includeModels: models)
}
private class func hondaPilot() -> ModelLine {
var models = [Model]()
models.append(Model(titled:"2016 Honda Pilot", description:"2016 Model", imageName: "pilot"))
return ModelLine(named:"Honda Pilot", includeModels: models)
}
HondaModelTableViewController.swift
import UIKit
class HondaModelTableViewController: UITableViewController {
var models: [Model]{
var modelLines = ModelLine.modelLines()
return modelLines[0].models
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return models.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("modelCell", forIndexPath: indexPath) as UITableViewCell
let model = models[indexPath.row]
cell.textLabel?.text = model.title
cell.detailTextLabel?.text = model.description
cell.imageView?.image = model.image
return cell
}
Это вопрос: вар modelLines = ModelLine.modelLines() возврата modelLines [0] .models. Тем не менее, ваш код выглядит немного ovecomplicated. – Lubos