Question on Cell Style


#1

In this chapter we programmatically created a TableView without a XOB/NIB and without a storyboard, correct?

So now I would like to change over in my cells to use the UITableViewCellStyleSubtitle version with its detail label, but am stumped.

The call to dequeueReusableCellWithIdentifier at the beginning of cellForRowAtIndexPath is NEVER nil, and it always creates a default single line cell. I see no opportunity when creating the tableviewcell to perform an initWithStyle:UITableViewCellStyleSubtitle which will set the cell type to what I want.

Without a storyboard or XIB how should I address this? Online I see posts that tell me to set the type in the storyboard or in the XIB - can this be done programmatically? I appreciate the advice in advance.

Or should I use the UITableViewCell subclassing customization in chapter 19? I guess that creates a custom class, and a XIB that contains only a TableViewCell? At a certain point why is this path better than having a storyboard or XIB for the TableView itself so I could take advantage of a prototype cell? Or is that just a new way of doing the chore at the start of chap19. I really would like to do this without subclassing etc if possible - it SHOULD be ez :wink:


#2

Is it not possible to use…

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewSubtitleCell" forIndexPath:indexPath]; if (!cell){ UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewSubtitleCell"]; }

and register the subtitleCell in viewDIdLoad…

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewSubtitleCell"];

I haven’t actually tried this, so maybe I’m missing something…


#3

THanks for the reply but you are missing something which I specifically mentioned :wink: dequeueReusableCellWithIdentifier NEVER returns nil in iOS7 even when NOT using a storyboard - as a result you never get to create your custom cell, the system has already created it for you and it is not the right type. That is the crux of the problem.


#4

Sorry yes I saw that as soon as I’d posted, not sure what the answer is apart from subclassing as you said…


#5

Ok how about this. If you use dequeueReusableCellWithIdentifier: instead of dequeueReusableCellWithIdentifier:atIndexPath: then it does return nil if a cell does not already exist. So you can then pass in your styled cell with the identifier, like this…

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewSubtitleCell"];
    if (cell) {
        NSLog(@"dequeued/n");
    }

    if(!cell){
    UITableViewCell *cellN = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewSubtitleCell"];
        cell=cellN;
    }

And this is without registering the reuse identifier elsewhere. Seems to work…


#6

Hmm, will give that a try. That may work since you don’t register the class in viewDidLoad. Did you check to see that it actually reuses the ones you create in subsequent dequeue requests? I will try that when I get a chance…


#7

I poked around to see if it had changed, but it does look like you still need to create a subclass. Or just use the old method. I think registerClass is used when you will be creating a custom cell (or don’t care it is going to be default style).

Remove in viewDidLoad.

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewSubtitleCell"]; 

Use in cellForRow

      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewSubtitleCell"];
      if (cell == nil)
         {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  reuseIdentifier:@"UITableViewSubtitleCell"];
         }

#8

Thanks for this thread. I’ve been working my way through the book (iOS Programming 4th Edition) and made the changes to BNRItemsViewController as specified on page 174. I had changed the initWithStyle parameter to UITableViewCellStyleSubtitle and got stuck on the problem discussed above.

Is there another way to change the CellStyle after a cell has been created?

Thanks or the help!