【问题标题】:accessing struct massive in UITableViewCell访问 UITableViewCell 中的大量结构
【发布时间】:2021-10-07 19:04:09
【问题描述】:

我用carName创建了一个集合视图,其中有5个,例如单击Mercedes(集合视图的单元之一)后,我想将标签文本设置为自己的值:carModel ( carName 和 carModel 都是相同的结构属性)在我已经创建的表格视图中,但我无法访问 carModel 数组

我尝试了for循环,但它返回一个错误

for i in cars.carModel {
  lbl.text = cars.carModel[i]
}

任何解决方案都会得到应用

//数据源文件

struct Cars {
    let carName:String
    let carModel:[String]
}
let cars:[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"])
]

//表格视图单元格文件

class TableViewCell: UITableViewCell {
    @IBOutlet var lbl: UILabel!
    func configure(with cars:Cars){
            lbl.text = cars.carName
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
}

//主视图控制器

class ViewController: UIViewController {

    
    @IBOutlet weak var collectionView: UICollectionView!
    
    //@IBOutlet weak var tableView
    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.dataSource = self
        collectionView.delegate = self
    }
}
extension ViewController: UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return cars.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as? CollectionViewCell
        cell?.setup(with: cars[indexPath.row])
        return cell!
    }
}

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)
        
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
        cell.configure(with: cars[indexPath.row])
        return cell
    }
    }

这是我的模拟器: img1 img2

我想要什么:expected result img

【问题讨论】:

    标签: swift uikit


    【解决方案1】:

    当您定义UITableViewDataSource 时,您将函数方法中的行数设置为返回一个常量 (5)。在cellForRowAt 函数中,您使用Cars 对象设置单元格。

    我认为您正在混合应该将哪些数据用于表格视图的数据源。

    对于集合视图数据源,您必须:

    • section 中的行数 函数应该返回:cars.count
    • Cell for item at 函数必须使用 Cars 实例配置 CollectionViewCell

    我认为您所缺少的只是保存在集合视图中选择了哪个单元格,以使用它为表格视图提供正确的数据。

    在集合视图委托中,当用户点击单元格时,您可以保存选定的 indexPath 或 Cars 实例,这将由表视图委托使用。我们称之为selectedCar

    那么对于表格视图数据源,您必须:

    • section 中的行数 函数应该返回:selectedCar.carModel.count
    • Cell for item at 函数必须为 TableViewCell 配置 CarModel 实例(即 String

    我在Github 创建了一个小项目作为示例,按预期工作。看看这个。希望这对您有所帮助。 Sample video 在 Youtube 上。

    注意:在 ViewController 中为 tableView 或 collectionView 设置 datasource 和 delegate 不是组织代码的最佳方式,但对于示例代码是可以的。

    【讨论】:

      猜你喜欢
      • 2010-11-13
      • 2019-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-25
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      相关资源
      最近更新 更多