UITableViewCell 不显示 detailTextLabel.text - Swift [英] UITableViewCell not showing detailTextLabel.text - Swift

查看:42
本文介绍了UITableViewCell 不显示 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 中的错误?),我们是这样解决的:

  1. 从故事板中删除原型单元

  2. 删除这一行:

var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

  1. 替换为以下代码行:

<前>让 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:

  1. Delete the prototype cell from your storyboard

  2. Remove this line:

var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

  1. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆