为iOS开发应用程序,我创建了一个结构,并且还创建了一个充满结构对象的数组。结构flavors
和descrip
有两个属性。我想从数组中的每个项目中获取每个flavor
属性,并使用它来填充表格视图单元格的标签。数组中有六个项目,所以我想要六个标签,以便它对应。所以标签应该是巧克力片,标签2蜂蜜,标签3糖,等等。所有建议和提示都非常感谢。
Import UIKit
class FlavorController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var flavorTable: UITableView!
struct cookieInfo {
var flavor: String
var descrip: String
}
var cookies = [cookieInfo(flavor: "Chocolate Chip", descrip: "Filled with gooey, milk chocholate! A classic!"), cookieInfo(flavor: "Honey", descrip: "Baked and drizzled with 100% pure honey, a must-have for sweet lovers!"), cookieInfo(flavor: "Sugar", descrip: "Simplicity meets savory, a sugar cookie topped with sweet icing!"), cookieInfo(flavor: "Peanut Butter", descrip: "A cookie infused with creamy peanut butter, the perfect cookie treat!"), cookieInfo(flavor: "Snickerdoodle", descrip: "Sugar cookie coated in cinnamon & sugar, baked to perfection!"),cookieInfo(flavor: "Shortbread", descrip: "An underrated yet flavorful cookie just like your grandma used to make!")]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return cookies.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FlavorCellTableViewCell
//this is where I want to the text of each flavorLabel to be the flavor property of the struct
cell.flavorLabel.text = ??
return cell
}
答案 0 :(得分:1)
这是简单的数组访问,后面只是字段访问。
let cookie = cookies[indexPath.row]
let flavor = cookie.flavor
cell.flavorLabel.text = flavor
或者更简单:
cell.flavorLabel.text = cookies[indexPath.row].flavor