Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableViewCell detailTextLabel未显示_Ios_Swift_Uitableview - Fatal编程技术网

Ios UITableViewCell detailTextLabel未显示

Ios UITableViewCell detailTextLabel未显示,ios,swift,uitableview,Ios,Swift,Uitableview,在Swift中使用UITableView及其UITableViewCell。我在显示UITableViewCell的detailTextLabel字段时遇到一些问题 我已经找到了这两个有用的帖子,但无法找到一个完全有效的解决方案: 以下是我正在使用的与我的问题相关的代码: override func viewDidLoad() { super.viewDidLoad() …………. theTableView = UITableView(frame: tmpFrame) ………

在Swift中使用
UITableView
及其
UITableViewCell
。我在显示
UITableViewCell
detailTextLabel
字段时遇到一些问题

我已经找到了这两个有用的帖子,但无法找到一个完全有效的解决方案:

以下是我正在使用的与我的问题相关的代码:

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
}
当我尝试使用
UITableViewCell
detailTextLabel
字段时,它不会显示。在网上搜索时,我知道我必须为单元格使用适当的样式(
UITableViewCellStyle
),但我不知道如何将这种更改集成到我的代码中。我看到的示例基于子类化
UITableViewCell
,我想我不需要子类化
UITableViewCell
,只需要使用
detailtextlab
字段。如果我错了,请告诉我


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

您已经注册了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,如果它不能将单元格出列。然后,您可以生成具有指定样式的自己的单元格。

我发现检查
单元格是否为
nil是不够的。
我添加了此附加检查:

var单元格:UITableViewCell?=tableView.dequeueReusableCell(标识符为:cellReuseIdentifier,for:indexPath)
如果单元格==nil | |单元格?.detailTextLabel==nil{
cell=UITableViewCell(样式:。副标题,reuseIdentifier:cellReuseIdentifier)
}

如果您使用的是
情节提要

  • 选择一个单元格
  • 转到属性检查器
  • 单击样式
  • 选择
    Subtitle

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

    cell=UITableViewCell(样式:。副标题,reuseIdentifier:“reuseIdentifier”)

    其中:CellStyle:Int

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

    您需要在您发布的第二个链接中使用代码,但只有在您删除TableView.registerClass(UITableViewCell.self,forCellReuseIdentifier:“reuseIdentifier”)
    行的使用之后才需要代码。我希望我已经给出了u+10。。!谢谢你
    tableView.dequeueReusableCellWithIdentifier
    在较好的情况下总是返回nil,在较差的情况下,
    detailTextLabel
    是nil。register tableview cell take cell=UITableViewCell()从未启动。这种方法没有什么问题,虽然它不是一种黑客行为,但它看起来就像一种黑客行为,如果你有一个自定义单元格,那么你所要做的就是:
    var cell=tableView.dequeueReusableCellWithIdentifier(“customReuseIdentifier”)如果cell==nil{cell=CustomCell(样式:.Value1,reuseIdentifier:“customReuseIdentifier”)}
    这并不像我认为的那样