gpt4 book ai didi

ios - 从结构中获取属性并用作标签的最佳方法?

转载 作者:行者123 更新时间:2023-11-28 15:10:34 25 4
gpt4 key购买 nike

为 iOS 开发应用程序时,我创建了一个结构,还创建了一个数组,其中填充了该结构的对象。结构flavorsdescrip 有两个属性。我想从数组中的每个项目中获取每个 flavor 属性,并用它来填充表格 View 单元格的标签。数组中有六个项目,所以我想要六个标签,以便它对应。所以标签 1 应该是巧克力片,标签 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

}

最佳答案

这是简单的数组访问,然后是简单的字段访问。

let cookie = cookies[indexPath.row]
let flavor = cookie.flavor
cell.flavorLabel.text = flavor

或者,更简单地说:

cell.flavorLabel.text = cookies[indexPath.row].flavor

关于ios - 从结构中获取属性并用作标签的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47735537/

25 4 0