user3545063 user3545063 - 1 year ago 74
Swift Question

UILabels and custom UITableView cells

I created a custom UITableView cell in my storyboard. Previously I used the "Subtitle" style for the cell, but I am now using a custom design made up with 2 UILabels.

How can I access these UILabels now? Before I used these two custom UILabels I would just use:

let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

cell.textLabel?.text = pollContent
cell.detailTextLabel?.text = dateString

in the

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


but since the cell style is now set to "custom" I can't really do that anymore.
The problem I have is that I don't really know how I could use IBOutlets to connect these two UILabels to my ViewController class somehow?

I also created a custom UITableViewCell class and connected the UILabels to that but how do I get the UILabel content for each cell from the ViewController?

So i cast the cell with my custom UITableViewCell now, and I added the following lines of code to my class:

self.textLabel = titleLabel
self.detailTextLabel = subTitleLabel

where titleLabel and subTitleLabel are both of my UILabels.

However I get the error message that .textLabel and .detailTextLabel are both get-only properties.

Thanks for all of your answers/suggestions. As you can probably guess, I am still a novice at programming!

I'll come back when I fix my problem.

Answer Source

You need to cast the cell as your custom Cell.

 let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? CustomTableViewCell

 cell.textLabel?.text = pollContent 
 cell.detailTextLabel?.text = dateString
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download