Swift自定义UITableViewCell标签总是为零

Dáv*_*tor 20 iphone cocoa-touch uitableview ios swift

我已经坚持这个问题好几天了,所以如果有人可以提供帮助,我会很高兴.我正在尝试创建一个动态的UITableView,为此我创建了一个自定义的UITableView子类,我也创建了一个自定义的UITableViewCell子类,因为我需要在每个单元格中使用几个UILabel和一个UIButton.单元格已创建,但问题是标签的值始终为零,因此单元格显示不正确. 这就是故事板的样子,这就是我在运行程序时看到的.

这是我的UITableViewCell子类:

import UIKit

class QuestionTableViewCell: UITableViewCell {

    @IBOutlet var student: UILabel!
    @IBOutlet var labDesk: UILabel!
    @IBOutlet var topic: UILabel!
    @IBOutlet var answers: UILabel!

}
Run Code Online (Sandbox Code Playgroud)

和我的UITableView子类:

import UIKit

class QuestionViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var table: UITableView!
     struct Question {
        var student: String
        var labDesk: String
        var topic: String
        var answered: String
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        table.estimatedRowHeight = 50
        table.dataSource = self
        table.delegate = self
        self.table.registerClass(QuestionTableViewCell.self, forCellReuseIdentifier: "cell")
    }

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

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

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

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell") as QuestionTableViewCell
        cell.student.text = "random string"
        cell.labDesk?.text = "25/A"
        cell.topic?.text = "string"
        cell.answers?.text = "3"
        return cell
    }
}
Run Code Online (Sandbox Code Playgroud)

ahm*_*mdx 53

尝试删除 self.table.registerClass(QuestionTableViewCell.self, forCellReuseIdentifier: "cell")

  • 我可以吻你❤️ (12认同)

小智 22

如果您正在使用带有nib的单元格,请确保使用表格视图注册单元格registerNib:forCellReuseIdentifier:.如果单元格只有一个类,那么使用registerClass:forCellReuseIdentifier:.

  • 如果您使用的是nib,那么您需要的确切语法是:`tableView.registerNib(UINib(nibName:"YOURNIBNAME",bundle:nil),forCellReuseIdentifier:"YOURREUSEID")` (5认同)

Amm*_*eeb 7

最重要的部分是将包含自定义单元格的 xib 注册到表视图中。因此在 viewDidLoad() 方法中添加以下代码。

let nib = UINib.init(nibName: "MyCustomCell", bundle: nil)    
self.tblUsers.register(nib, forCellReuseIdentifier: "MyCustomCell")
Run Code Online (Sandbox Code Playgroud)


Ian*_*Ian 6

首先,如果类存在于Interface Builder中,则无需注册该类.

其次,你应该dequeueReusableCellWithIdentifier:forIndexPath而不是dequeueReusableCellWithIdentifier.

第三,UITableViewController已经有一个被称为属性的属性,tableView所以不需要创建一个IBOutlet,table因为UITableViewController已经处理了这个.它也符合UITableViewDataSource,UITableViewDataSource因此这些是无关紧要的.

第四,不要设置为其table设置的属性tableView.

第五,cell.labDesk.text = ""足够,不需要任选.

如果所有的IBOutlet都已连接,Cell Identifiers正确设置,并且这些修订已经完成,它将起作用.

class QuestionTableViewCell: UITableViewCell {

    @IBOutlet var student: UILabel!
    @IBOutlet var labDesk: UILabel!
    @IBOutlet var topic: UILabel!
    @IBOutlet var answers: UILabel!

}


class QuestionViewController: UITableViewController {

    struct Question {
        var student: String
        var labDesk: String
        var topic: String
        var answered: String
    }

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

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

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

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

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as QuestionTableViewCell
        cell.student.text = "random string"
        cell.labDesk.text = "25/A"
        cell.topic.text = "string"
        cell.answers.text = "3"
        return cell
    }
}
Run Code Online (Sandbox Code Playgroud)