1

I wish for each cell to have the detailTextLabel showing.

The cell is instantiated (at cellForRowAtIndexPath:) with :

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

And i tryed to set the style type with:

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                   reuseIdentifier:@"Cell"] autorelease];
}

(My xcode gave ARC warning with the auto release, so i also tryed with it omitted. Same result though)

I'm a little confused. Obviously without the cell == nil, the first section of code is in vain, yet with it, the cell never has the detailTextLabel showing. (Yes, cell.detailTextLabel.text is being set correctly)

How would i go about this?

Update: as i am using storyboard i was able to achieve the required result by setting the Cell style to 'subtitle'. However this question of how to go about it programmatically still stands

Simon.
  • 1,886
  • 5
  • 29
  • 62
  • If you have ARC enabled than `autorelease` is not required. – Kampai Jan 07 '15 at 10:35
  • in the place use custom cell – Anbu.Karthik Jan 07 '15 at 10:38
  • 1
    Are you using storyboard? if yes have a look at this thread http://stackoverflow.com/questions/15424453/why-is-my-uitableviewcell-not-showing-detailtextlabel-in-any-style – riyaz Jan 07 '15 at 10:43
  • 1
    haha, i found the storyboard solution as soon as you posted the comment. thank you anyway! although this has resolved the issue, the question of how to complete this programmatically still stands – Simon. Jan 07 '15 at 10:47
  • If you **ditch** the story board and do it programatic, your code should work fine. P.S remove autorelease – riyaz Jan 07 '15 at 11:01
  • 2
    If you are using `dequeueReusableCellWithIdentifier:forIndexPath:`, then it's not possible to have `cell == nil`. Apple guarantees that you have a `UITableViewCell` instance when that method returns. If you want to use Storyboards, then your solution is **THE** solution. Alternatively, you can do it the "old" way (`dequeueReusableCellWithIdentifier:`) and **then** you can check for `cell == nil` and create a new `UITableViewCell` if needed. – mbm29414 Jan 07 '15 at 19:31
  • I understand. I think the option of doing things, either via storyboard or via code was unclear for me, however I have a better understanding from this. if you wish to put your comment into an answer i'll happily accept it for this question – Simon. Jan 08 '15 at 09:17

2 Answers2

0

Changing to the following code should work when doing this programmatically. (thanks really to mbm29414)

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                  reuseIdentifier:@"Cell"];
}
ryanmmanalo
  • 101
  • 6
0

The modern way of creating a table cell is to register the cell, and then dequeue it with the index path. The problem is, that if you register UITableViewCell you will always get a cell of type default.

The solution is to subclass UITableViewCell and set the style in it. For example:

class SubtitleTableViewCell: UITableViewCell {

  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError()
  }
}

Now, use your subclass when registering.

let table = UITableView(frame: .zero, style: .plain)
table.register(DebugTableViewCell.self, forCellReuseIdentifier: identifier)
Jason Moore
  • 7,169
  • 1
  • 44
  • 45