【问题标题】:set TableView cell label struct's array设置 TableView 单元格标签结构的数组
【发布时间】:2021-11-28 06:40:35
【问题描述】:

我的结构有两个属性,一个是carName,它的类型是字符串,另一个是carModel,它是一个字符串数组,我创建了CollectionViewCollectionView,然后单击特定的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
    }
    }

【问题讨论】:

    标签: ios swift uitableview uikit


    【解决方案1】:

    这是错误的方法 首先你已经设置了

    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
    }
    

    【讨论】:

    • 你的代码返回错误
    • 什么样的错误?
    • Cannot convert value of type 'String' to expected argument type 'Cars' cell.configure(with: carsArray[indexPath.row])
    • 在tableview单元格中将参数类型更改为String func configure(with: String)
    • Value of type 'Cars' has no member 'carModels' 和这个在vc?.carsArray = cars[indexPath.row].carModels 这一行
    【解决方案2】:

    你的结构

    struct Cars {
        let carName:String
        let carModel:[String]
    }
    

    第一个 ViewController - CollectinViewControler 类

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      • 2017-05-09
      • 2020-12-28
      • 1970-01-01
      • 2015-03-31
      • 2021-11-27
      • 2020-07-06
      相关资源
      最近更新 更多