簡體   English   中英

使用 Storyboard 創建的自定義 UITableViewCell 顯示沒有 label 數據 - Swift

[英]Custom UITableViewCell created using Storyboard shows no label data - Swift

問題:我有一個簡單的 UITableViewController 並想在自定義單元格中顯示 label。 我創建了一個簡單的 UITableViewController,其中第 1 部分和第 4 行是硬編碼的。 在 storyboard 中,使用新的 label 自定義單元格,給它一個標識符並在 DemoTableViewCell 中映射一個出口。 以下是所有不同代碼片段的快照。 對我來說,在 function 中分配的 label 文本仍然沒有顯示。

class DemoTableViewCell: UITableViewCell {

    @IBOutlet weak var demoLabel: UILabel!

}

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
}

單元重用標識符

分配了一個自定義類

自定義單元類中的插座

輸出截圖

故事板標簽

有人可以讓我知道我做錯了什么嗎?

我的假設是您沒有為 label 設置約束,因為您的 label 位於具有自定義高度的單元格的中心,但在您的模擬器中,您的單元格高度僅為默認高度 44 pt

一些選擇

  1. 您可以嘗試將label 設置為單元格頂部

  2. 將 storyboard 中的 tableview 行高設置為等於您的自定義單元格高度在此處輸入圖像描述

  3. 正確的步驟是使用 label position 設置 label 約束以使單元格高度正確

Rest 除了自動布局之外,您的所有代碼都很好。 您需要對 demoLabel 應用以下約束:

在此處輸入圖像描述

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM