UITableViewCell 不显示 detailTextLabel.text - Swift [英] UITableViewCell not showing detailTextLabel.text - Swift
问题描述
未显示详细信息(副标题)文本.但是,数据是可用的,因为当添加 println() 调用时,它会将 Optional("data") 与预期数据一起打印到控制台.在故事板中,UITableViewController 设置为适当的类,Table View Cell Style 设置为'Subtitle',重用标识符设置为'cell'.如何获取要显示的字幕信息?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell {var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCelldispatch_async(dispatch_get_main_queue(), { () -> Void incell.textLabel.text = self.myArray[indexPath.row]["title"] as?细绳cell.detailTextLabel?.text = self.myArray[indexPath.row]["subtitle"] as?细绳println(self.myArray[indexPath.row]["subtitle"] as? String)//预期的数据出现在控制台中,但不在 iOS 模拟器的表格视图单元格中.})返回单元格}
同样的问题(从我读过的内容来看,也许是 iOS 8 中的错误?),我们是这样解决的:
从故事板中删除原型单元
删除这一行:
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
- 替换为以下代码行:
<前>让 cellIdentifier = "单元格"var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as?UITableViewCell如果单元格 == 零 {cell = UITableViewCell(样式:UITableViewCellStyle.Value2,reuseIdentifier:cellIdentifier)}
Swift 3.1 更新
let cellIdentifier = "Cell";var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)如果单元格 == 零 {cell = UITableViewCell(样式:UITableViewCellStyle.value2,reuseIdentifier:cellIdentifier)}
Swift 4.2 更新 - 简化
let cell = UITableViewCell(样式:UITableViewCell.CellStyle.value2,reuseIdentifier:cellId")
Swift 5 更新 - 简化版
let cell = UITableViewCell(style:.value2,reuseIdentifier:cellId")
The detail (subtitle) text does not appear. The data are available, though, because when a println() call is added, it prints Optional("data") to the console with the expected data. In the storyboard, the UITableViewController is set to the proper class, the Table View Cell Style is set to 'Subtitle', and the reuse identifier is set to 'cell'. How can I get the subtitle information to display?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
dispatch_async(dispatch_get_main_queue(), { () -> Void in
cell.textLabel.text = self.myArray[indexPath.row]["title"] as? String
cell.detailTextLabel?.text = self.myArray[indexPath.row]["subtitle"] as? String
println(self.myArray[indexPath.row]["subtitle"] as? String)
// The expected data appear in the console, but not in the iOS simulator's table view cell.
})
return cell
}
Same issue here (from what I've read, perhaps a bug in iOS 8?), this is how we worked around it:
Delete the prototype cell from your storyboard
Remove this line:
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
- Replace with these lines of code:
let cellIdentifier = "Cell" var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell if cell == nil { cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: cellIdentifier) }
Update for Swift 3.1
let cellIdentifier = "Cell"
var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.value2, reuseIdentifier: cellIdentifier)
}
Update for Swift 4.2 - Simplified
let cell = UITableViewCell(style: UITableViewCell.CellStyle.value2, reuseIdentifier: "cellId")
Update for Swift 5 - Simplified
let cell = UITableViewCell(style: .value2, reuseIdentifier: "cellId")
这篇关于UITableViewCell 不显示 detailTextLabel.text - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!