gpt4 book ai didi

swift - 如何在tableview swift中设置两个部分的数据

转载 作者:行者123 更新时间:2023-11-30 10:36:49 25 4
gpt4 key购买 nike

我想为马自达版本 2 添加 id: 1,2,3 部分,为马自达版本 1.5 添加 4,5,6,但我在这里遇到错误:

cell.textLabel?.text = headline.title 
cell.detailTextLabel?.text = headline.text

error: Value of type '[(id: Int, title: String, text: String, image: String, color: UIColor)]' has no member 'title'

下面是我的代码

import UIKit

struct Headline {

var id : Int
var title : String
var text : String
var image : String
var color: UIColor!


}

var headlines = [
[(id: 1, title: "Mazda 3", text: "Price: 8500000000", image: "mazdared", color: UIColor.red),
(id: 2, title: "Mazda 3", text: "Price: 8000000000", image: "mazdablue", color: UIColor.blue),
(id: 3, title: "Mazda 3", text: "Price: 9000000000", image: "mazdablack", color: UIColor.black)] ,

[(id: 4, title: "Mazda 3", text: "Price: 10000000000", image: "mazdared", color: UIColor.red),
(id: 5, title: "Mazda 3", text: "Price: 9500000000", image: "mazdablue", color: UIColor.blue),
(id: 6, title: "Mazda 3", text: "Price: 9000000000", image: "mazdablack", color: UIColor.black)]
]

class CarTableViewController: UITableViewController {

override func numberOfSections(in tableView: UITableView) -> Int {
return headlines.count
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return headlines[section].count
}

let headerTitles = ["Mazda Version 2", "Mazda Version 1.5"]
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section < headerTitles.count {
return headerTitles[section]
}

return nil
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)

let headline = headlines[indexPath.row]
cell.textLabel?.text = headline.title
cell.textLabel?.textColor = UIColor.white
cell.detailTextLabel?.text = headline.text
cell.detailTextLabel?.textColor = UIColor.white
cell.backgroundColor = headline.color
cell.imageView?.image = UIImage(named: headline.image)


return cell
}

}

最佳答案

在这里,我对您的代码进行了一些修改以使其正常工作。

第 1 步:

替换

var headlines = [
[(id: 1, title: "Mazda 3", text: "Price: 8500000000", image: "mazdared", color: UIColor.red),
(id: 2, title: "Mazda 3", text: "Price: 8000000000", image: "mazdablue", color: UIColor.blue),
(id: 3, title: "Mazda 3", text: "Price: 9000000000", image: "mazdablack", color: UIColor.black)] ,

[(id: 4, title: "Mazda 3", text: "Price: 10000000000", image: "mazdared", color: UIColor.red),
(id: 5, title: "Mazda 3", text: "Price: 9500000000", image: "mazdablue", color: UIColor.blue),
(id: 6, title: "Mazda 3", text: "Price: 9000000000", image: "mazdablack", color: UIColor.black)]
]

var headlines = [

[
Headline(id: 1, title: "Mazda 3", text: "Price: 8500000000", image: "mazdared", color: UIColor.red),
Headline(id: 2, title: "Mazda 3", text: "Price: 8000000000", image: "mazdablue", color: UIColor.blue),
Headline(id: 3, title: "Mazda 3", text: "Price: 9000000000", image: "mazdablack", color: UIColor.black)
],
[
Headline(id: 4, title: "Mazda 3", text: "Price: 10000000000", image: "mazdared", color: UIColor.red),
Headline(id: 5, title: "Mazda 3", text: "Price: 9500000000", image: "mazdablue", color: UIColor.blue),
Headline(id: 6, title: "Mazda 3", text: "Price: 9000000000", image: "mazdablack", color: UIColor.black)
]
]

这里我已经正确初始化了结构。

第 2 步:

在您的cellForRowAt方法中替换

let headline = headlines[indexPath.row]

let headline = headlines[indexPath.section][indexPath.row]

因为在您的代码中,当您将 headlines[indexPath.row] 存储在 headline 对象中时,它将成为一个多维数组,在这里不起作用。所以我纠正了这一点。

关于swift - 如何在tableview swift中设置两个部分的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57818271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com