質問

When I create a tableview with custom cell I use this

static NSString *CellIdentifier = @"mycell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

and it work fine (I work with storyboard and ARC)

but sometimes I see this control:

if (!cell){
    //here the alloc of a custom cell
}

should I use this only when I have a custom cell with its class? If I use storyboard do I need it? thanks

役に立ちましたか?

解決 2

If you are using prototype cells with storyboard, you will never get inside that if statement, so you don't need it. Otherwise you do, for example when you instantiate the cell from a xib file, then you would do that inside the if statement.

他のヒント

dequeueReusableCellWithIdentifier:forIndexPath: will raise an exception if there is no cell to dequeue. So there is no point in checking if cell is nil. There is no way that cell will be nil after this call. You will either have a valid cell or an exception is raised.

That means you have to register a cell (through storyboard or in code) for this call to work.

There is a second (or old) way to dequeue cells, dequeueReusableCellWithIdentifier:, without the forIndexPath:. That's how we did it in earlier versions when there was no way to register cells. If no cell is registered for the identifier this method will return nil, and you have to create a cell in code.

I would stick with registering cells and dequeueReusableCellWithIdentifier:forIndexPath:. You don't need the if (!cell) part in this case. And the exception will make sure that you not forget to register your cells.

If you are using storyboards use prototype cells. In this tutorial there is a whole section about protoype cells: http://www.raywenderlich.com/50308/storyboards-tutorial-in-ios-7-part-1

Its not necessary if we are using storyboard.Checkout this link

http://useyourloaf.com/blog/2012/06/07/prototype-table-cells-and-storyboards.html

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top