【发布时间】:2016-06-24 03:02:44
【问题描述】:
我创建了一个 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
}
}
我在情节提要上收到以下错误:
从 TableViewController 到 UILabel 的价格出口无效。插座不能连接到重复的内容。
我理解错误的含义,但是我认为 MyCustomTableViewCell 类意味着价格变量是动态的,因此不会尝试重复静态内容。
任何帮助将不胜感激:)
【问题讨论】:
-
您是否注册了自定义单元 XIB 以供重复使用?
-
@DookieMan 不,我没有,我该怎么做?
-
self.tableView.registerNib(UINib(nibName: "CellName", bundle: nil), forCellReuseIdentifier: "CellIdentifier")
标签: ios swift uitableview custom-cell