1

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 {}

method

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?

UPDATE: 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.

UPDATE 2: 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.

UPDATE 3: Again, thanks for your help! It finally works now.

user3545063
  • 681
  • 8
  • 17

5 Answers5

2

You need to cast the cell as your custom Cell.

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


 cell.customLabel1.text = pollContent 
 cell.customLabel2.text = dateString
Dan Levy
  • 3,931
  • 4
  • 28
  • 48
rmickeyd
  • 1,551
  • 11
  • 13
  • Thanks for the quick response! I must be missing something, the UILabels still just display the default text. How would the cell know that textLabel and detailTextLabels are referencing my UILabels? – user3545063 Apr 27 '16 at 17:30
  • Make sure your cell in Storyboards is set as the custom cell. Then you create outlets to the file for the custom cell. – rmickeyd Apr 27 '16 at 17:39
  • .textLabel and .detailTextLabel are both get-only properties. – user3545063 Apr 27 '16 at 17:43
  • Please post your full custom cell file. You should not have to to self.titleLabel etc. – rmickeyd Apr 27 '16 at 17:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/110418/discussion-between-rmickeyd-and-user3545063). – rmickeyd Apr 27 '16 at 18:00
  • @rMikeyD messaged you back! – user3545063 Apr 27 '16 at 18:06
  • 1
    @user3545063 I just updated this answer (waiting for peer review). The code showed that you should use cell.textLabel? and cell.detailTextLabel?. Calling that would still show the text in the cell's default labels which you can't see because your cell is set to "Custom" and you don't get an error because textLabel and detailTextLabel are not force unwrapped (has ? instead of !). You will need to use cell.customLabel to access the custom label's text. Hope this helps! – Dan Levy Apr 27 '16 at 19:25
  • Thanks @DanL, I could already solve the problem with rMickeyD 's help. And yes, this was the actual fault that caused the errors. I just didn't know that you could directly reference the custom UILabels (that were declared in the UIViewTableCell) in the ViewController. It works, because the cell gets casted by the custom viewtablecell and I didn't know that. – user3545063 Apr 27 '16 at 21:15
0

1.You need to create a CustomUITableViewCell class that extends the UITableViewCell.

2.Add Your IBOutlets to this CustomUITableViewCell.

3.Set the Cell class as your CustomUITableViewCell that you just created in your storyboard.

4.change this

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

    cell.textLabel?.text = pollContent 
    cell.detailTextLabel?.text = dateString
Cloy
  • 2,141
  • 23
  • 32
0

Please try:

self.textLabel.text = titleLabel
self.detailTextLabel.text = subTitleLabel

For setting text insinde these labels.

Budyn
  • 134
  • 1
  • 4
0

Seems you must set cell identifier also in the Attribute Inspector :

enter image description here

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
0

Set some unique values to your uilabels tag property.

Get them later in

cellForItemAtIndexPath

like so:

UILabel *label = (UILabel*)[cell viewWithTag:yourTagNumber];

BTW: Don't try to assign textLabel and detailTextLabel when the style of your cell is Custom. They are available only if style is Subtitle. Otherwise, they are nil.

efimovdk
  • 368
  • 4
  • 16