[英]UITableView Cells Are Showing Only 1 Cell
我一直在嘗試構建一個基本的Swift應用來顯示汽車。 我有一個UITableView設置。 目前,我定義了兩輛車,但是由於某種原因,只有1輛車正在顯示。
模型模型
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)
}
本田ModelTableViewController.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
}
在
var models: [Model]{
var modelLines = ModelLine.modelLines()
return modelLines[0].models
}
您只返回數組中的第一個元素。
您只是返回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.
}
由於您將模型設置為二維數組,因此以后可能會實現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.
}
在HondaModelTableViewController
您僅返回modelLines
數組的第一個對象。
將return modelLines[0].models
為:
for model in modelLines {
return model.models
}
在numberOfRowsInSection中,您返回modelLines [0]中的模型數
您已將本田CR-V加載到modelLine [0]中,將本田Pilot加載到modelLines [1]中
您只返回一行,因為這就是您指定的數據量。 如果您希望兩者都出現在同一個表中,則需要整理構造函數
這些部分需要解決:
// 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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.