I'm having trouble creating custom table table cells in swift using Xcode6 (beta 4). More precisely, I cannot access my custom UILabels inside the cell, since they are never initialized.
Here, as I have all the settings:
I pretended to be in a storyboard that contains a tabular view with a prototype cell:

The view is connected to the MyCoursesTableViewController class and the cell (with the courseCell identifier) in CourseTableViewCell. I inserted both classes below (only with corresponding bits of code):
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
if let titleLabel = cell.titleLabel {
} else {
cell.textLabel.text = "Course Title"
}
return cell
}
}
A class NavToggleTableViewControlleris just an ordinary base class, which I use for all view controllers and do not affect the result.
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)
}
}
, ( ):

tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> CourseTableViewCell, UILabels. - cell.titleLabel.text = "Course Title", :
fatal error: unexpectedly found nil while unwrapping an Optional value
- ? , !