Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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不显示标签数据-Swift_Ios_Swift_Uitableview - Fatal编程技术网

Ios 使用序列图像板创建的自定义UITableViewCell不显示标签数据-Swift

Ios 使用序列图像板创建的自定义UITableViewCell不显示标签数据-Swift,ios,swift,uitableview,Ios,Swift,Uitableview,问题: 我有一个简单的UITableViewController,希望在自定义单元格中显示标签。我创建了一个简单的UITableViewController,其中第1节和第4行是硬编码的。在故事板中,使用新标签自定义单元格,给它一个标识符,并在DemobileViewCell中映射一个出口。下面是所有不同代码片段的快照。对我来说,函数中指定的标签文本仍然无法显示 class DemoTableViewCell: UITableViewCell { @IBOutlet weak var

问题: 我有一个简单的UITableViewController,希望在自定义单元格中显示标签。我创建了一个简单的UITableViewController,其中第1节和第4行是硬编码的。在故事板中,使用新标签自定义单元格,给它一个标识符,并在DemobileViewCell中映射一个出口。下面是所有不同代码片段的快照。对我来说,函数中指定的标签文本仍然无法显示

class DemoTableViewCell: UITableViewCell {

    @IBOutlet weak var demoLabel: UILabel!

}


有人能告诉我我做错了什么吗?

我的假设是您没有为该标签设置约束,因为您的标签位于具有自定义高度的单元格中心,但在模拟器中,您的单元格高度仅为默认高度44磅

有一些选择吗

  • 您可以尝试将标签设置到单元格顶部

  • 将序列图像板中的tableview行高度设置为等于自定义单元格高度

  • 正确的步骤是设置标签约束,使单元格高度与标签位置匹配


  • 除自动布局外,其余所有代码都正常。您需要对demoLabel应用以下约束:


    在StoryAborableViewDataSource中显示您的demoLabel方法是否正确调用?必须设置tableView的数据源和委托?Hello刚刚添加了标签外观的故事板快照。由于它是UITableViewController类,我不确定是否需要显式设置委托和数据源。我对Swift和IOS非常陌生,所以我可能错了。如果我将代码编写为cell.textLabel?.text=“Override Label”,它将正确打印。但它不是一个自定义标签。这是默认的单元格标签,很有效。好吧,很好,很明显,在我给它一些限制的时候,它就起了作用。所以,我现在可能还需要设置单元格高度,因为我需要放入一个图像和两个垂直对齐的标签。非常感谢您的快速响应。是的,因为没有约束,tableviewcell将具有默认高度,如果希望单元格高度取决于内容,请不要设置行高度,因为如果设置高度,所有单元格都将具有相同的高度
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 4
    }
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell : DemoTableViewCell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! DemoTableViewCell
    
        cell.demoLabel.text = "Override Label"
        // Configure the cell...
    
        return cell
    }