UILabels in a custom UITableViewCell is never initialized

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:

How it looks in storyboard

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

        // cell is not nil

        if let titleLabel = cell.titleLabel {
            // never gets here
        } 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)
    }
}

, ( ):

Utilities pane

tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> CourseTableViewCell, UILabels. - cell.titleLabel.text = "Course Title", :

fatal error: unexpectedly found nil while unwrapping an Optional value

- ? , !

+4
2

UITableView Swift, Xcode. :

    override func tableView(tableView:UITableView!, heightForRowAtIndexPath indexPath:NSIndexPath)->CGFloat {
    return 44
}

( ), . .

, - : registerClass:forCellReuseIdentifier. , .

+1

, , -. XCode 8.3.2 (8E2002), , ​​ . , OP

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 44
}

. , , viewDidLoad:

    override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()


    // REMOVE THIS v v v v v        
    self.tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "MyCustomCell")
    // REMOVE THIS ^ ^ ^ ^ ^ 
    // Do whatever you want here…
}

heightForRowAt indexPath , , .

, !

0

All Articles