我一直在尝试使用StackOverFlow中的所有方法来实现这个功能,但我没有运气。
UITableView cell.textLabel
正确显示,但未显示cell.detailTextLable
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "cell"
var cell : UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellIdentifier)
}
cell!.textLabel?.text = "hello"
cell!.detailTextLabel?.text = "world"
return cell!
}
我注意到在其他教程中,最后3个单元格行没有textLabel
或detailTextLabel
上的选项,但如果我没有选项,XCode会抛出错误。也许这表明其他地方出现了错误?
编辑:我是如何创建实际的UITableView
的var dataTable : UITableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
dataTable.frame = CGRectMake(0, 300, 200, 200)
dataTable.delegate = self
dataTable.dataSource = self
dataTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.view.addSubview(dataTable)
}
答案 0 :(得分:2)
当您注册UITableViewCell类时,您可以保证获得一个单元格,它将是没有detailTextLabel的基本单元格。因此,您的“if cell == nil”子句将永远不会运行。只需删除寄存器类语句,您的代码就可以正常工作。