Home > Back-end >  set TableView cell label struct's array
set TableView cell label struct's array

Time:10-08

I've struct with two properties one is carName which's type is string and the other one is carModel which is an array of strings, I've created CollectionView of carName's and after clicking the specific CollectionView cell, Main view controller (which is CollectionView) goes to the other one which is TableView and in that view I want to set labels of carModel. here are the pictures for better understanding:CollectionView -> TableView from the last picture you can see that my code doesn't work. expected result:image3. I tried for loop ,switch and case but doesn't seem to work. any solutions?

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
    }
    }

CodePudding user response:

this is wrong approach first you have set

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)
           
}

you should make a var carsArray on tableviewcontroller like that

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
}
  • Related