SwiftのカスタムUITableViewCellラベルは常にnilです 質問する

SwiftのカスタムUITableViewCellラベルは常にnilです 質問する

何日もこの問題に悩まされているので、誰か助けていただけたら本当に嬉しいです。動的な UITableView を作成しようとしています。そのためにカスタム UITableView サブクラスを作成し、カスタム UITableViewCell サブクラスも作成しました。これは、各セルに複数の UILabel と UIButton が必要なためです。セルは作成されますが、問題はラベルの値が常に nil であるため、セルが正しく表示されないことです。これはストーリーボードがどのようになっているか、そしてこれはプログラム実行中に表示されるもの。

これが私の UITableViewCell サブクラスです:

import UIKit

class QuestionTableViewCell: UITableViewCell {

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

}

そして私の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
    }
}

ベストアンサー1

削除してみてくださいself.table.registerClass(QuestionTableViewCell.self, forCellReuseIdentifier: "cell")

おすすめ記事