xcode - 设置具有正确详细信息的自定义单元格样式,但它不显示详细信息文本标签?

标签 xcode swift detailtextlabel uitableview

我使用的是 Xcode7.2.1 iOS9.2 SDK。

set the custom cell style with Right detail

  1. 注册单元格 Nib :

代码:

self.TbuserList.registerNib(UINib.init(nibName: "UserCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "idUserList")
  • 设置单元格的detailTextLabel文本:
  • 代码:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("idUserList",forIndexPath: indexPath ) as! UserCell
        //print(self.users)
        cell.textLabel?.text = self.users[indexPath.row]["nickname"] as? String
        cell.detailTextLabel?.text = (users[indexPath.row]["isConnected"] as! Bool) ? "Online" : "Offline"
        cell.detailTextLabel?.textColor = (users[indexPath.row]["isConnected"] as! Bool) ? UIColor.greenColor() : UIColor.redColor()
        return cell
    }
    

    enter image description here

    注意:当我将单元格样式从“右侧详细信息”更改为“副标题”或“左侧详细信息”时,就可以了。

    最佳答案

    由于您使用的是从 xib 文件创建的自定义单元格,因此我建议避免尝试使用默认单元格元素(textLabel 和detailTextLabel),而只需添加创建所需单元格所需的 View 。您可以向标准单元添加其他 View ,但确保您的 View 与现有标准单元 View 配合使用可能会稍微复杂一些。如果标准单元格类型适合您的需求,您可以向 tableView 注册 UITableViewCell 类,而不是自定义 xib 文件。查看 Apple docs 中的自定义单元格部分

    关于xcode - 设置具有正确详细信息的自定义单元格样式,但它不显示详细信息文本标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35700830/

    相关文章:

    IOS 多个左右对齐在同一行

    objective-c - 为什么 detailTextLabel 不可见?

    ios - 如何创建将在本地显示 float 窗口的ANE

    ios - 访问 UITableView Cell 标签文本并在方法中使用它

    xcode - 在Xcode 4中将项目添加到工作区时遇到问题

    ios - 如何在我的应用程序中使用密码锁定场景?

    swift 无法显示 imageview 底部的 View

    c++ - 重复的 symbol_main

    Swift 如何在所有 uiTextfield 上退出第一响应者