UITableViewCell没有显示detailTextLabel.text - Swift

kur*_*ple 32 uitableview detailtextlabel 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 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
}
Run Code Online (Sandbox Code Playgroud)

onC*_*ion 46

你的代码看起来很好.只需转到故事板并选择tableview的单元格 - >现在转到属性检查器并选择Subtitle样式.

请按照以下屏幕截图进行操作.

更改此选项

希望它有所帮助..


wpe*_*rse 31

同样的问题(从我读过的,也许是iOS 8中的一个错误?),这就是我们解决它的方法:

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

  2. 删除此行:

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

  1. 替换为这些代码行:
let cellIdentifier = "Cell"

var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell
if cell == nil {
    cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: cellIdentifier)
}

更新Swift 3.1,Xcode版本8.3.3:

let cellIdentifier = "Cell"

var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
if cell == nil {
    cell = UITableViewCell(style: UITableViewCellStyle.value2, reuseIdentifier: cellIdentifier)
}
Run Code Online (Sandbox Code Playgroud)

Swift 4.2的更新 - 简化

let cell = UITableViewCell(style: UITableViewCell.CellStyle.value2, reuseIdentifier: "cellId")
Run Code Online (Sandbox Code Playgroud)

  • 这仍然是一个问题! (3认同)

kes*_*hav 15

如果您仍想使用故事板中的原型单元格,请选择TableViewcell样式作为字幕.它会工作.

  • 谢谢.重新编写代码后,"Subtitle"选项有效. (2认同)

Col*_*ris 6

如果以编程方式执行此操作而不在接口构建器中使用单元格,则此代码的工作方式类似于Swift 2.0+

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    yourTableView.delegate = self
    yourTableView.dataSource = self
    yourTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "subtitleCell")

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return yourTableViewArray.count
}

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    let cell: UITableViewCell = yourTableView.dequeueReusableCellWithIdentifier("subtitleCell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = "the text you want on main title"
    cell.detailTextLabel?.text = "the text you want on subtitle"

    return cell
}
Run Code Online (Sandbox Code Playgroud)

  • 定义新单元将花费如此多的内存?我认为`dequeueReusableCellWithIdentifier`将通过重用单元格来帮助保存内存 (3认同)

Hug*_*ner 5

如果在尝试将文本设置为非 nil 值时将文本设置为 nil,则包含文本的实际视图将丢失。这是在 iOS8 中引入的。尝试改为设置为空白@" "字符。

看到这个:UITableViewCell 的字幕不会更新


Zou*_*ssi 5

在此处输入图片说明

试试这个对我有用(swift 5)

let cell = UITableViewCell(style: .value1, reuseIdentifier: "cellId")
cell.textLabel.text = "Déconnexion"
cell.imageView.image = UIImage(named: "imageName")
Run Code Online (Sandbox Code Playgroud)

目标c:

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellId"];
Run Code Online (Sandbox Code Playgroud)