我一直在尝试构建一个基本的Swift应用来展示汽车。我有一个UITableView设置。目前我定义了两辆车,但由于某种原因只显示了一辆。
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
}
答案 0 :(得分:1)
在
var models: [Model]{
var modelLines = ModelLine.modelLines()
return modelLines[0].models
}
你只返回数组中的第一个元素。
答案 1 :(得分:1)
您只是返回第一个ModelLine数组,其中只有一辆车。
class func modelLines() -> [ModelLine]{
//index 0 index 1
return[self.hondaCRV(), self.hondaPilot()]
}
var models: [Model]{
var modelLines = ModelLine.modelLines()
return modelLines[0].models //Only index 0 being called.
}
由于您的模型设置为2维数组,因此self.hondaPilot()
可能会在稍后实施以返回多个Model
,因此我建议您设置ModelLines
在部分中,您的Model
作为该部分中的行。
var modelLines : [ModelLines] { //Use modelLines instead so you have access to ALL cars of all modelLines.
return ModelLine.modelLines()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections4
return modelLines.count //This will give you 2 sections.
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return modelLines[section].count //Gives you number of models in each modelLines section.
}
答案 2 :(得分:0)
在HondaModelTableViewController
中,您只返回modelLines
数组的第一个对象。
将return modelLines[0].models
替换为:
for model in modelLines {
return model.models
}
答案 3 :(得分:0)
在numberOfRowsInSection中,返回modelLines [0]中的模型数量
您已将本田CR-V装入modelLine [0],将本田Pilot装入modelLines [1]
您只返回一行,因为这是您指定的数据量。如果您希望两者都出现在同一个表中,则需要对构造函数进行排序
答案 4 :(得分:0)
需要解决这些问题:
// Part I:
// var models: [Model]{
// var modelLines = ModelLine.modelLines()
// return modelLines[0].models
// }
let models = ModelLine.modelLines()
// Part II:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
let model = models[indexPath.row].models[0]
cell.textLabel?.text = model.title
cell.detailTextLabel?.text = model.description
return cell
}