4thSpace
4thSpace

Reputation: 44310

How to use detailTextLabel when initializing cell in code?

I don't have a prototype cell in IB. I'm initializing the cell in code.

let mycell = UITableViewCell(style: .Default, reuseIdentifier: "reuseIdentifier")
mycell.textLabel!.text = "main text"
mycell.detailTextLabel!.text = "test"

The mycell.textLabel!.text call works fine. But the mycell.detailTextLabel!.text call crashes the app because mycell.detailTextLabel is nil. Is there something else I need to do for that part to work?

Upvotes: 0

Views: 84

Answers (1)

knopch1425
knopch1425

Reputation: 759

I ran into the same problem - this helped.

How to Set UITableViewCellStyleSubtitle and dequeueReusableCell in Swift?

Use something like:

cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,
                reuseIdentifier: "identifier")

Upvotes: 1

Related Questions