/ / Swift niestandardowa etykieta UITableViewCell jest zawsze zerowa - iOS, iPhone, uitableview, cocoa-touch, swift

Swift niestandardowa etykieta UITableViewCell jest zawsze nil - ios, iphone, uitableview, cocoa-touch, swift

Utknąłem z tym problemem od kilku dni, więcByłbym naprawdę szczęśliwy, gdyby ktoś mógł pomóc. Próbuję utworzyć dynamiczny UITableView, dla którego utworzyłem niestandardową podklasę UITableView i utworzyłem również niestandardową podklasę UITableViewCell, ponieważ potrzebuję kilku UILabels i UIButton w każdej komórce. Komórka jest tworzona, ale problem polega na tym, że wartość etykiet jest zawsze zerowa, dlatego komórka nie jest wyświetlana poprawnie. To jest, jak wygląda scenorys i to jest co widzę podczas uruchamiania programu.

Oto moja podklasa UITableViewCell:

import UIKit

class QuestionTableViewCell: UITableViewCell {

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

}

i moja podklasa 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
}
}

Odpowiedzi:

34 dla odpowiedzi nr 1

Spróbuj usunąć self.table.registerClass(QuestionTableViewCell.self, forCellReuseIdentifier: "cell")


15 dla odpowiedzi nr 2

Jeśli używasz komórki ze stalówką, upewnij się, że rejestrujesz komórkę w widoku tabeli za pomocą registerNib:forCellReuseIdentifier:. Jeśli komórka ma tylko klasę, użyj registerClass:forCellReuseIdentifier:.


5 dla odpowiedzi nr 3

Po pierwsze, nie musisz rejestrować klasy, jeśli istnieje w Interface Builder.

Po drugie, powinieneś dequeueReusableCellWithIdentifier:forIndexPath zamiast dequeueReusableCellWithIdentifier.

Trzeci, UITableViewController ma już właściwość o nazwie tableView więc nie ma potrzeby tworzenia IBOutlet do table ponieważ UITableViewController już to obsługuje. Jest również zgodny z UITableViewDataSource i UITableViewDataSource więc te są obce.

Po czwarte, nie ustawiaj właściwości dla table ustaw je dla tableView.

Piąty, cell.labDesk.text = "" jest wystarczający, nie ma potrzeby, aby był opcjonalny.

Jeśli wszystkie Twoje IBOutlety są podłączone, identyfikatory komórek są poprawnie ustawione i te poprawki są wprowadzone, to zadziała.

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
}
}

1 dla odpowiedzi nr 4

Dla tych, którzy wciąż próbują to rozgryźć po wypróbowaniu wszystkich możliwych rozwiązań:

Odłącz / Podłącz ponownie IBOutlets w swoich Storyboardach powinno załatwić sprawę!


0 dla odpowiedzi № 5

Mogę się tu spóźnić, ale właśnie rozwiązałem podobny problem.

Upewnij się, że ustawiłeś identyfikator w InterfaceBuilder na swoim UITableViewCell.

wprowadź opis obrazu tutaj