UITableViewCell detailTextLabel没有显示出来

Mic*_*hel 10 uitableview ios swift

在Swift中使用a UITableView和它UITableViewCell的.我有一些问题显示了一个detailTextLabel领域UITableViewCell.

我已经发现这两个有用的职位,但无法得到充分有效的解决方案: 迅速detailTextLabel没有显示出来, 如何设置UITableViewCellStyleSubtitle和dequeueReusableCell斯威夫特?

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

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
}
Run Code Online (Sandbox Code Playgroud)

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

我也从上面提到的帖子中尝试了一些东西,但没有任何效果,因为我想要的.

Pat*_*ick 27

您已注册UITableViewCell

theTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
Run Code Online (Sandbox Code Playgroud)

这意味着当你打电话时

tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
Run Code Online (Sandbox Code Playgroud)

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

因为你想要一个自定义样式,你需要做一些事情:

去掉

dequeueReusableCellWithIdentifier

更换

dequeueReusableCellWithIdentifier

以下内容

theTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
Run Code Online (Sandbox Code Playgroud)

dequeueReusableCellWithIdentifier如果它不能使一个单元格出列,那么这里发生的将返回nil.然后,您可以使用指定的样式生成自己的单元格.

注意:写了没有Xcode,我是Swift的新手,如果它不编译就道歉.

  • 我希望我给了你+10 ..!谢谢..! (2认同)

sta*_*ich 5

如果您正在使用Storyboard

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