我有两个属性的结构,一个是carName
,它的类型是字符串,另一个是carModel
,它是字符串数组,我创建了carName
的CollectionView
,点击特定的CollectionView
单元格后,主视图控制器(这是CollectionView
)转到另一个是TableView
,在那个视图中我想设置carModel
的标签。为了更好地理解,这里有一些图片:表视图从最后一张图中,您可以看到我的代码不起作用。预期结果:image3。我尝试了循环,开关和情况,但似乎不工作。有解决方案吗?
struct Cars {
let carName:String
let carModel:[String]
}
let cars = [
Cars(carName: "Mercedes", carModel: ["S Class","A Class", "B Class"]),
Cars(carName: "BMW", carModel: ["X5","X6","X7"]),
Cars(carName: "Ford", carModel: ["Fuison","Focus","Mustang"]),
Cars(carName: "Toyota", carModel: ["Camry", "Corolla"]),
Cars(carName: "Hyundai", carModel: ["Elantra"])
]
///TableViewCell code
@IBOutlet var lbl: UILabel!
func configure(with cars:Cars){
for i in 0..<cars.carModel{
lbl.text = cars.carModel[i] // Error is here
}
}
extension ViewController:UICollectionViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(identifier: "TableViewController") as? TableViewController
self.navigationController?.pushViewController(vc!, animated: true)
a = indexPath.row
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
print(indexPath.row)
cell.configure(with: cars[a])
return cell
}
}
这是错误的做法首先你要设置
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return cars.count
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(identifier: "TableViewController") as? TableViewController
vc.carsArray = cars[indexPath.row].carModels
self.navigationController?.pushViewController(vc!, animated: true)
}
你应该在tableviewcontroller上创建一个像这样的var carsArray
class TableViewContrller {
var carsArray:[String] = [] {
didSet { tableView.reloadData()
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return carsArray.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
print(indexPath.row)
cell.configure(with: carsArray[indexPath.row])
return cell
}
你的struct
struct Cars {
let carName:String
let carModel:[String]
}
第一个ViewController - collectinviewcontroller类
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
let cars = [
Cars(carName: "Mercedes", carModel: ["S Class","A Class", "B Class"]),
Cars(carName: "BMW", carModel: ["X5","X6","X7"]),
Cars(carName: "Ford", carModel: ["Fuison","Focus","Mustang"]),
Cars(carName: "Toyota", carModel: ["Camry", "Corolla"]),
Cars(carName: "Hyundai", carModel: ["Elantra"])
]
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
self.cars.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellid", for: indexPath as IndexPath) as! YourCollectionViewCell
cell.label.text = self.cars[indexPath.row].carName
cell.backgroundColor = UIColor.cyan
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "TableViewController") as? SecondViewController
vc?.carModel = self.cars[indexPath.row].carModel
self.navigationController?.pushViewController(vc!, animated: true)
}
}
你的第二个视图控制器- TableView
class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var carModel: [String]?
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
self.carModel?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell") as! UITableViewCell
cell.textLabel?.text = self.carModel?[indexPath.row]
return cell
}
}