2

I'm doing a simples app using Storyboard that a have a View with a UITableView with a UITableViewCell that do the navigation to another UIView.

So a have to code to populate the cell on the table view.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"SampleCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        NSLog(@"cai no init da cell");
    }

    GPItem *item = [self.items objectAtIndex:indexPath.row];

    cell.textLabel.text = @"Post";
    cell.detailTextLabel.text = item.imageURL;

    return cell;
}

I realised that the code if (cell == nil) { ... never executes so I really need to do that on uses the cell from Storyboard?

Thanks.

1
  • 1
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; this makes cell not nil Commented Feb 13, 2013 at 23:34

3 Answers 3

5

You are correct; that code is guaranteed to return a non-nil cell if you are using a storyboard. Also, in iOS 6, the new call dequeueReusableCellWithIdentifier:forIndexPath: never returns nil. See the discussion in my book:

http://www.apeth.com/iOSBook/ch21.html#_registering_a_cell_class

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks man. So and what are you think about using storyboards? Should use or not?
I use storyboards sometimes, but I also know how generate my interface and my view controllers in code or using old-style nib files. Storyboards are great as long as you understand them. They are not magic. You still have to know what's really going on. Again, see my book: apeth.com/iOSBook/ch19.html#_storyboards
0

If you've declared your UITableViewCell in table view's prototype cells it's already allocated and just needs to be dequeued. If you're using a custom UITableViewCell subclass, then you must check if it's nil and allocate new entities when necessary.

2 Comments

That's not precisely right. A custom UITableViewCell subclass can still be the prototype cell from the storyboard so it will never be nil.
I don't remember saying custom cells cannot be prototyped. I said if the cell is not prototyped, it should be allocated. Context man, context.
0

Nope you don't need that code when using a cell made in your storyboard.

It is probably best to remove this code so that you crash nice and early if the identifier you gave to the cell in interface builder and the identifier you use in code ever drift. This snippet will mask this error and just provide a cell that you most likely was not intending to have.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.