设置 TableView 单元格标签结构的数组
set TableView cell label struct's array
我构造了两个属性,一个是 carName
,类型是字符串,另一个是 carModel
,它是一个字符串数组,我创建了 CollectionView
carName
并在单击特定的 CollectionView
单元格后,主视图控制器(CollectionView
)转到另一个 TableView
,在该视图中我想设置 carModel
的标签。为了更好地理解,这里有图片:CollectionView -> TableView
从最后一张图片你可以看到我的代码不起作用。预期结果: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 Cars {
let carName:String
let carModel:[String]
}
第一个 ViewController - CollectinViewControler class
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)
}
}
你的第二个 Viewcontroller - 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
}
}
我构造了两个属性,一个是 carName
,类型是字符串,另一个是 carModel
,它是一个字符串数组,我创建了 CollectionView
carName
并在单击特定的 CollectionView
单元格后,主视图控制器(CollectionView
)转到另一个 TableView
,在该视图中我想设置 carModel
的标签。为了更好地理解,这里有图片:CollectionView -> TableView
从最后一张图片你可以看到我的代码不起作用。预期结果: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 Cars {
let carName:String
let carModel:[String]
}
第一个 ViewController - CollectinViewControler class
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)
}
}
你的第二个 Viewcontroller - 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
}
}