简体   繁体   English

Swift 自定义 UITableViewCell label 始终为零

[英]Swift custom UITableViewCell label is always nil

I've been stuck with this problem for days, so I'd be really happy if someone could help.我已经被这个问题困扰了好几天,所以如果有人可以提供帮助,我会非常高兴。 I'm trying to create a dynamic UITableView, for which I created a custom UITableView subclass and I've created a custom UITableViewCell subclass as well, because I need several UILabels and a UIButton in each cell.我正在尝试创建一个动态 UITableView,为此我创建了一个自定义 UITableView 子类,并且我还创建了一个自定义 UITableViewCell 子类,因为我需要几个 UILabels 和一个 ZA4F6654A7772842BZEC18 中的每个单元格。 The cell is created, but the problem is that the value of the labels is always nil, hence the cell isn't displayed properly.单元格已创建,但问题是标签的值始终为零,因此单元格无法正确显示。 This is , how the storyboard looks like, and this is what I see while running the program.这就是storyboard 的样子,这是我在运行程序时看到的。

Here's my UITableViewCell subclass:这是我的 UITableViewCell 子类:

import UIKit

class QuestionTableViewCell: UITableViewCell {

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

}

and my UITableView subclass:和我的 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
    }
}

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

If you're using a cell with a nib then make sure that you are registering the cell with the table view using registerNib:forCellReuseIdentifier: .如果您使用带有笔尖的单元格,请确保使用registerNib:forCellReuseIdentifier:将单元格注册到表格视图中。 If the cell just has a class then use registerClass:forCellReuseIdentifier: .如果单元格只有一个类,则使用registerClass:forCellReuseIdentifier:

First, you don't have to register the class if it exists in Interface Builder.首先,如果类存在于 Interface Builder 中,则不必注册该类。

Second, you should dequeueReusableCellWithIdentifier:forIndexPath instead of dequeueReusableCellWithIdentifier .其次,您应该dequeueReusableCellWithIdentifier:forIndexPath而不是dequeueReusableCellWithIdentifier

Third, UITableViewController already has a property called tableView so there is no need to make an IBOutlet to table as UITableViewController already handles this.第三, UITableViewController已经有一个名为tableView的属性,因此无需将 IBOutlet 设置为table因为 UITableViewController 已经处理了这个问题。 It also conforms to the UITableViewDataSource and UITableViewDataSource so these are extraneous.它也符合UITableViewDataSourceUITableViewDataSource所以这些都是无关紧要的。

Fourth, don't set the properties for table set them for tableView .第四,不要为table设置属性为tableView设置它们。

Fifth, cell.labDesk.text = "" is sufficient, no need to make it optional.第五, cell.labDesk.text = ""就够了,没必要让它可选。

If all your IBOutlets are hooked up, Cell Identifiers correctly set, and these revisions are made, it will work.如果您的所有 IBOutlets 都连接好,正确设置了 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
    }
}

The most important part is to register the xib containing the custom cell with the table view.最重要的部分是在表格视图中注册包含自定义单元格的 xib。 Therefore add the following code in viewDidLoad() method.因此在 viewDidLoad() 方法中添加以下代码。

let nib = UINib.init(nibName: "MyCustomCell", bundle: nil)    
self.tblUsers.register(nib, forCellReuseIdentifier: "MyCustomCell")

I might be late here, but I just solved a similar problem.我可能来晚了,但我刚刚解决了一个类似的问题。

Make sure you've set the Identifier in InterfaceBuilder on your UITableViewCell.确保您已在 UITableViewCell 上的 InterfaceBuilder 中设置了标识符。

在此处输入图片说明

For those who are still trying to figure this out after trying all those possible solutions:对于那些在尝试了所有可能的解决方案后仍然试图解决这个问题的人:

Disconnect/Reconnect the IBOutlets in your Storyboards should do the trick!断开/重新连接故事板中的 IBOutlets 应该可以解决问题!

Don't forget to add:不要忘记添加:

tableView?.register(UINib(nibName: "xyz",
                           bundle: nil),
                    forCellReuseIdentifier: "abc")

If you are using a table cell with Xib.如果您在 Xib 中使用表格单元格。 you need to register your cell with ..你需要用..注册你的手机

register(_:forCellReuseIdentifier:)

If you haven't added constraints for the label then they will not be created though the custom cell is created.如果您尚未为标签添加约束,则即使创建了自定义单元格,也不会创建它们。 Make sure you added some constraints.确保您添加了一些约束。

Make sure that the selected cell is in the right " module " and if necessary, inherit:确保所选单元格在正确的“ module ”中,如有必要,继承:

在此处输入图片说明

If not, your IBOutlets will be nil.如果没有,您的IBOutlets将为零。

Step 1: Remove datasource and delegate reference form storyboard.第 1 步:删除数据源委托引用表格 storyboard。

Step 2: In viewDidLoad add,第 2 步:在 viewDidLoad 添加,

tableView.delegate = self
tableView.dataSource = self

Step 3: In tableview UITableViewDataSource cellForRowAt function, add your cell the given way.第 3 步:在 tableview UITableViewDataSource cellForRowAt function 中,以给定的方式添加您的单元格。

let cell = tableView.dequeueCell(ofType: YourCellName.self)
cell.yourCellFunction()
return cell

Note 1: dequeueCell(ofType...) is calling the below function internally.注 1: dequeueCell(ofType...) 在内部调用下面的 function。 you don't need to use it directly.你不需要直接使用它。

func dequeueCell<T: UITableViewCell>(ofType type: T.Type) -> T  {
}

Important: You don't need to provide any "Resporation ID" or "Reuse Identifier" for cell.重要提示:您不需要为单元格提供任何“Resporation ID”或“Reuse Identifier”。 It works with your cell name.它适用于您的单元格名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM