【发布时间】:2014-07-15 10:44:11
【问题描述】:
我正在尝试在表格视图的自定义单元格中显示内容。 我从一个主细节应用程序开始,并尝试对其进行自定义。 我使用自定义单元创建了一个新的 xib,并将其连接到一个类并创建了所需的插座。
这是 MasterView 控制器(删除了我没有编辑的所有功能)
import UIKit
class MasterViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
var nipName=UINib(nibName: "SimpleTableCell", bundle:nil)
self.tableView.registerNib(nipName, forCellReuseIdentifier: "cell")
self.tableView.registerClass(SimpleTableCell.self, forCellReuseIdentifier: "cell")
//Create Add Button
let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "insertNewObject:")
self.navigationItem.rightBarButtonItem = addButton
loadInitialData()
tableView.reloadData()
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as SimpleTableCell
cell.loadItem(user:"user1", hoop: "hoop1", post: "There is text in the table")
return cell
}
}
这是自定义单元格的类
import UIKit
class SimpleTableCell : UITableViewCell{
@IBOutlet var userLabel: UILabel
@IBOutlet var hoopLabel: UILabel
@IBOutlet var postLabel: UILabel
func loadItem(#user: String, hoop: String, post: String) {
userLabel = UILabel()
hoopLabel = UILabel()
postLabel = UILabel()
userLabel.text = user
hoopLabel.text = hoop
postLabel.text = post
}
}
但是,一旦 userLabel 的文本即将更改,我总是在 loadItem 函数中得到“无法解开 optional.none” - 我没有得到它,因为我在尝试之前初始化了标签添加文本 - 还是我错了?
感谢您的帮助。
【问题讨论】:
-
无需在设置文本之前初始化标签。如果您有笔尖,则将标签从笔尖连接到插座
标签: ios swift tableview custom-cell