【问题标题】:UITableViewCell detailTextLabel not showing upUITableViewCell detailTextLabel 未显示
【发布时间】:2015-12-05 03:59:55
【问题描述】:

在 Swift 中使用 UITableView 及其 UITableViewCell(s)。我在显示 UITableViewCelldetailTextLabel 字段时遇到了一些问题。

我已经找到了这两个有用的帖子,但无法获得完全有效的解决方案: swift detailTextLabel not showing up How to Set UITableViewCellStyleSubtitle and dequeueReusableCell in Swift?

这是我正在使用的代码,与我的问题相关:

override func viewDidLoad() {
    super.viewDidLoad()

………….
    theTableView = UITableView(frame: tmpFrame)
………….
    theTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
    theTableView.dataSource = self
    theTableView.delegate = self
    self.view.addSubview(theTableView)
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
    cell.backgroundColor = UIColor.clearColor()
    cell.textLabel?.text = "THE-TEXT-LABEL"
    cell.detailTextLabel?.text = "KIJILO" // This does not show up.
    return cell
}

当我尝试使用UITableViewCelldetailTextLabel 字段时,它没有出现。在网上搜索,我知道我必须为单元格使用正确的样式(UITableViewCellStyle),但我不知道如何将该更改集成到我的代码中。我看到的示例是基于子类化UITableViewCell,我想我不需要子类化UITableViewCell 只是为了使用detailTextLabel 字段。如果我错了,请告诉我。

我也尝试了上面提到的帖子中的一些东西,但没有任何效果。

【问题讨论】:

  • 您需要您发布的第二个链接中的代码,但前提是您删除了theTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier") 行的使用。

标签: ios swift uitableview


【解决方案1】:

你已经注册了 UITableViewCell

theTableView.registerClass(UITableViewCell.self,
 forCellReuseIdentifier: "reuseIdentifier")

这意味着当你调用时

tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", 
forIndexPath: indexPath)

将使用 UITableViewCellStyleDefault 样式自动为您创建一个。

因为您想要自定义样式,所以您需要做一些事情:

删除

theTableView.registerClass(UITableViewCell.self, 
forCellReuseIdentifier: "reuseIdentifier")

替换

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

以下

var cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier")
if cell == nil {
    cell = UITableViewCell(style: .Value1, reuseIdentifier: "reuseIdentifier")
}

这里发生的情况是dequeueReusableCellWithIdentifier 如果无法将单元格出列,将返回 nil。然后,您可以生成自己的单元格,并提供指定的样式。

【讨论】:

  • 我希望我能反刍给你+10..!谢谢你..!
  • tableView.dequeueReusableCellWithIdentifier 在更好的情况下总是返回 nil,在更坏的情况下 detailTextLabel 是 nil
  • 解决了。注册tableview cell take cell = UITableViewCell() 从不调用
  • 赞成。这种方法没有任何问题,虽然它不是 hack,但它只是 看起来 像 hack,
  • 如果您有自定义单元格,那么您所要做的就是:var cell = tableView.dequeueReusableCellWithIdentifier("customReuseIdentifier") if cell == nil { cell = CustomCell(style: .Value1, reuseIdentifier: "customReuseIdentifier") }
【解决方案2】:

如果您使用的是Storyboard

  1. 选择一个单元格
  2. 转到属性检查器
  3. 点击样式
  4. 选择Subtitle

【讨论】:

    【解决方案3】:

    我发现仅仅检查cell 是不是nil. 是不够的,我添加了这个额外的检查:

    var cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath)
    if cell == nil || cell?.detailTextLabel == nil {
        cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellReuseIdentifier)
    }
    

    【讨论】:

    • 这和我想的不一样
    【解决方案4】:

    很好的答案,谢谢!我唯一要补充的是,如果您想要不同样式的细节视图(我正在寻找 .subtitle),您需要执行以下操作:

    cell = UITableViewCell(style: .subtitle, reuseIdentifier: "reuseIdentifier")

    其中:CellStyle : Int

        case `default` = 0
    
        case value1 = 1
    
        case value2 = 2
    
        case subtitle = 3
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-16
      • 2012-03-17
      • 2013-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多