Os UILabels no UITableViewCell personalizado nunca são inicializados
Estou tendo problemas para criar células de exibição de tabela personalizadas rapidamente com o Xcode6 (beta 4). Mais precisamente, não consigo acessar (desembrulhar) meus UILabels personalizados dentro da célula, pois eles nunca são inicializados.
Aqui está como eu tenho tudo configurado:
Fiz uma exibição no storyboard, que contém uma exibição de tabela com uma célula protótipo:
A visualização está conectada a uma classe MyCoursesTableViewController e a célula (com identificador courseCell) a CourseTableViewCell. Colei as duas classes abaixo (com apenas os bits relevantes do código):
MyCoursesTableViewController.swift
import UIKit
class MyCoursesTableViewController: NavToggleTableViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(CourseTableViewCell.self, forCellReuseIdentifier: "courseCell")
}
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! {
var cell : CourseTableViewCell = tableView.dequeueReusableCellWithIdentifier("courseCell", forIndexPath: indexPath) as CourseTableViewCell
// cell is not nil
if let titleLabel = cell.titleLabel {
// never gets here
} else {
cell.textLabel.text = "Course Title"
}
return cell
}
}
oNavToggleTableViewController
A classe é apenas uma classe base comum que eu uso para todos os controladores de exibição e não afeta o resultado.
CourseTableViewCell.swift
import UIKit
class CourseTableViewCell: UITableViewCell {
@IBOutlet weak var courseIcon: UIImageView!
@IBOutlet weak var teacherIcon: UIImageView!
@IBOutlet weak var studentsCountIcon: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var studentsCountLabel: UILabel!
@IBOutlet weak var teacherLabel: UILabel!
init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
Abaixo está uma imagem de como eu configurei a célula no painel de utilitários (no storyboard):
O problema surge dentro da funçãotableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> CourseTableViewCell
quando eu quiser acessar os UILabels. Se eu fosse colocar algo comocell.titleLabel.text = "Course Title"
Eu recebi a seguinte mensagem de erro:
fatal error: unexpectedly found nil while unwrapping an Optional value
Onde estou fazendo coisas erradas? Gostaria de receber qualquer ajuda, obrigado!