gpt4 book ai didi

ios - UITableView Swift 2 中的自定义单元格

转载 作者:行者123 更新时间:2023-11-30 13:37:48 25 4
gpt4 key购买 nike

我已经创建了一个 UITableView,并希望动态创建单元格来保存名称、持续时间和价格等信息。

我有以下代码:

class MyCustomTableViewCell: UITableViewCell {
@IBOutlet weak var price: UILabel!
}

class TableViewController: UITableViewController {
var data = [["name1","amount1","duration1"],["Name2","amount2","duration2"],["name3","amount3","duration3"]]


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCustomTableViewCell", forIndexPath: indexPath) as! MyCustomTableViewCell

cell.textLabel?.text = "\(data[indexPath.row][0])"
cell.detailTextLabel?.text = "\(data[indexPath.row][1])"
cell.price.text = "\(data[indexPath.row][2])"
return cell
}

}

我在 Storyboard 上收到以下错误:

从 TableViewController 到 UILabel 的价格导出无效。 Outlet 无法连接到重复的内容。

我理解错误的含义,但是我认为 MyCustomTableViewCell 类意味着价格变量是动态的,因此不会尝试重复静态内容。

如有任何帮助,我们将不胜感激:)

enter image description here

最佳答案

我从 Askash 那里得到了这个答案,但是我的代码似乎有点令人困惑,所以我想我会发布我的答案,以防其他人遇到问题。

步骤 1. 确保使用以下代码创建一个 TableViewController 并确保将其设置在 Storyboard 上:

class TableViewController: UITableViewController {
var data = [["name1","amount1","duration1"],["Name2","amount2","duration2"],["name3","amount3","duration3"]]


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell

cell.textLabel?.text = "\(data[indexPath.row][0])"
cell.detailTextLabel?.text = "\(data[indexPath.row][2])"
cell.price.text = "£\(data[indexPath.row][2])"
return cell
}

}

在 Storyboard上单击 TableViewController,单击“显示身份检查器”并将“TableViewController”粘贴到类部分中: enter image description here

第 2 步 创建一个新的 TableViewCell 类。- 转到文件 > 新建 > 文件... > IOS 源 > Cocoa Touch Class > 下一步并创建一个具有 UITableViewCell 子类的 TableViewCell 类: enter image description here

第3步将TableViewCell分配给原型(prototype)单元并设置恢复ID。

单击 Storyboard 中的原型(prototype)单元,单击显示“属性检查器”并将“TableViewCell”粘贴到类部分和标识符类型“cell”中: enter image description here

步骤 4 在“TableViewCell”类中创建标签“Price”的导出。 (使用 Ctrl + 拖动)并将其命名为“价格”。

TableViewCell 中的代码应如下所示:

TableViewCell 类:UITableViewCell {

@IBOutlet weak var price: UILabel!


override func awakeFromNib() {
super.awakeFromNib()
}

override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}

}

关于ios - UITableView Swift 2 中的自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35896658/

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