簡體   English   中英

Swift 當單元格有值時 TableViewCell 為空

[英]Swift TableViewCell empty when cell has values

我有一個帶有一些簡單標簽的自定義單元格和一個 UIImage。 一切似乎都設置正確,單步調試調試器顯示所有內容都在獲取值,甚至在調試器中使用打印顯示標簽有文本。 但是我的表格視圖在執行時仍然是空的。 我已經看這個太久了,無法弄清楚問題所在。

這是單元格代碼

class CurrentFileCell: UITableViewCell {


@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var statusImage: UIImageView!
@IBOutlet weak var dateLabel: UILabel!


var currentContent: AircraftContent! {
    didSet{
        setStyles(Constants.appStyleSetting)

        self.nameLabel.text = currentContent.contentName
        self.dateLabel.text = currentContent.contentStatus
        self.statusImage.image = UIImage(named: "color_label_circle_green")

    }
}

override func awakeFromNib() {
    super.awakeFromNib()
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
}

private func setStyles(settings: StyleSettings) {
    let tableFont = UIFont(name: settings.bodyFont, size: CGFloat(settings.tableFontSize))
    nameLabel.font = tableFont
    dateLabel.font = tableFont

    // Text color
    let tableFontColor = settings.tableFontColor
    nameLabel.textColor = tableFontColor
    dateLabel.textColor = tableFontColor
}

這是里面有一個 tableview 的 ViewController 代碼。

class CurrentFilesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var content: AircraftContent?

@IBOutlet weak var currentFiles: UILabel!
@IBOutlet weak var downloadingLabel: UILabel!
@IBOutlet weak var readyLabel: UILabel!

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.delegate = self
    self.tableView.dataSource = self

    self.content = loadContent()
    setStyles(Constants.appStyleSetting)
    //self.tableView.reloadData()
            // Do any additional setup after loading the view.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("CurrentFileCell", forIndexPath: indexPath) as? CurrentFileCell
    cell?.currentContent = content

    return cell!
}


func loadContent() -> AircraftContent {
    return (NSKeyedUnarchiver.unarchiveObjectWithFile(AircraftContent.ArchiveURL.path!) as? AircraftContent)!
}

private func setStyles(settings: StyleSettings) {
    let titleFont = UIFont(name: settings.bodyFont, size: CGFloat(settings.titleFontSize))
    let key = UIFont(name: settings.bodyFont, size: CGFloat(settings.tableFontSize))
    currentFiles.font = titleFont
    downloadingLabel.font = key
    readyLabel.font = key

    // Text color
    let titleFontColor = settings.titleFontColor
    currentFiles.textColor = titleFontColor
    downloadingLabel.textColor = titleFontColor
    readyLabel.textColor = titleFontColor

}

以下是一些顯示單元格不為空的調試位置的圖像,以及打印出具有值但在仿真期間未顯示的 label。

http://imgur.com/a/dBkpe

這是顯示原型單元的圖像。 該單元格具有正確的 class 集以及標識符。

http://imgur.com/PKtFTeQ

最后一張顯示原型單元格鏈接到 CurrentFileCell 中的標簽的圖像。

http://imgur.com/nW0QUjM

對此有任何幫助將不勝感激。 我嘗試過重新創建所有內容,但仍然感到困惑,因為似乎一切都應該是這樣。

您必須為表格視圖實現'heightForRowAtIndexPath'方法

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

        let height:CGFloat = 75
        return height

}

您可能會考慮注冊自定義類,因為它似乎沒有注冊。 您可以通過在View Controller的viewDidLoad中使用以下代碼來實現。

tableView.registerClass(CurrentFileCell.self, forCellReuseIdentifier: "cell")

如果您使用外部筆尖,則應改用registerNib如下所示:

tableView.registerNib(UINib(name:"ReplaceWithYourNibName", bundle: nil), forCellReuseIdentifier: "ReuseIdentifier")

當然,用適當的值替換ReplaceWithYourNibNameReuseIdentifier 另外,如果您的筆尖位於其他捆綁包中,請指定該值而不是nil(nil默認為主捆綁包)。

但是,請勿同時使用registerClassregisterNib因為將使用您最后調用的那個,並且它們被設計為互斥的。 每當您創建自定義UITableViewCell時,除非在情節提要中明確設置了它,否則都必須使用兩者中的任一個才能起作用。

另外,您可以改為使用原型單元來定義您的自定義單元,我相信它將自動注冊該單元。 但是只有在不使用原型單元的情況下,才能確保使用registerClassregisterNib

祝好運! 希望這可以幫助!

如果您的單元格是靜態單元格,則需要在UITableViewDataSource中注釋掉以下方法:

/*    override func numberOfSections(in tableView: UITableView) -> Int {
 // #warning Incomplete implementation, return the number of sections
    return 0
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 0
} */

我遇到過同樣的問題。 數據有值,單元格行顯示為空。 我想通過添加

contentView.addSubview(titleLabel)

正在設置單元格值的位置。

暫無
暫無

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

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