我一直在尝试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)
}
当你注册UITableViewCell类时,你保证会得到一个单元格,它将是没有detailTextLabel的基本单元格。因此,您的"if cell == nil"子句将永远不会运行。只需删除 register 类语句,您的代码应该可以正常工作。