Async-
Async-

Reputation: 3289

IBOutlet is nil, but it is connected in storyboard (Swift 2)

I am trying to show some content in the cell of the UITableView. The program does reach the cellForRowAtIndexPath:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell: EventTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("eventCell") as! EventTableViewCell

    let date = self.eventArray[indexPath.row].startTime
    let calendar = NSCalendar.currentCalendar()
    let minutes = calendar.component(NSCalendarUnit.Minute, fromDate: date)
    var minutesString: String
    if (minutes == 0) {
        minutesString = "00"
    } else {
        minutesString = String(calendar.component(NSCalendarUnit.Minute, fromDate: date))
    }
    let hours = calendar.component(NSCalendarUnit.Hour, fromDate: date)
//this next line of code works, i see cell text:
//  cell.textLabel?.text = self.eventArray[indexPath.row].title + " - \(hours):\(minutesString)"

//these lines do not work, see empty cells:
    cell.cellLable?.text = self.eventArray[indexPath.row].title + " - \(hours):\(minutesString)"
    cell.textField?.text = self.eventArray[indexPath.row].notes
    return cell
}

I have properly connected the outlets: enter image description here

If I set breakpoint, the cell appears to be the EventTableViewCell, but both cellLabel and textField are nil: enter image description here

My table view connections look like this:

enter image description here

Connection inspector for the eventCell here: enter image description here

I have also made Content View background color blue, but it seems like I don't see the whole Content view in my cell.

My custom cell class looks like this: enter image description here

Upvotes: 2

Views: 1102

Answers (2)

Helge Becker
Helge Becker

Reputation: 3243

cell.textLabel works since you inherit the UITabelViewCell Class. You are getting a UITabelViewCell as reference - that is granted.

Create a strong reference to your label within the scope. Place a breakpoint and validate what you are actually getting as cell. (indeed the correct subclass of yours or something generic.)

If the cell object is correct, but you don't get the label it must be outlet related. In some cases in Swift I came upon the need to change the OUTLET for an UIElement from weak to strong.

If yes, only the properties of the label remain as cause. Give the label a background color. It's layout is actually visible? (eg.: hidden = NO, alpha = 1, frame/constrains render it visible and so on.)

Upvotes: 0

Dan Loughney
Dan Loughney

Reputation: 4677

Check that you have the correct identifier for your cell in IB. Let cell... is returning nil so that appears to be your problem.

let cell: EventTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("eventCell") as! EventTableViewCell 

Upvotes: 1

Related Questions