Where does the indexPath of dequeueReusableCellWithIdentifier:forIndexPath: get used?

Apple's documentation says of the indexPath parameter:

The index path specifying the location of the cell. The data source receives this information when it is asked for the cell and should just pass it along. This method uses the index path to perform additional configuration based on the cell's position in the table view.

But register(Class|Nib):forCellReuseIdentifier: only specifies the reuse identifier to use, not the section or a set of index paths.

I thought perhaps UITableViewCell had some way of getting its hands on the index path, so it could, say, round its corners if in the first row of a section, but I'm not seeing it. At creation time, all it gets is its style and reuse identifier ( initWithStyle:reuseIdentifier: ); at reuse time, all it gets told is prepareForReuse .

Seeing as the old dequeueReusableCellWithIdentifier: is still supported, what sort of index-path-based configuration could it possibly be doing, if it can't rely on having the chance to do it, anyway?

I checked the Table View Programming Guide, but it hasn't been updated since iOS 5.


According to WWDC 2012 Session 200 - What's New In Cocoa Touch,

If you use - dequeueReusableCellWithIdentifier:forIndexPath: to dequeue your cell, it will be the right size and you'll be able to do layout inside your cell's contentView .

That's pretty much a quote from Chris Parker, a UIKit Engineer.

Up until iOS 6, you had to subclass your UITableViewCell and override - layoutSubviews if you wanted to make layout adjustments. From encapsulation point of view, this might still be the better solution – however, sometimes you just need a tiny adjustment, and now you can do that in - tableView:cellForRowAtIndexPath: instead.


The most important difference between dequeueReusableCellWithIdentifier: and dequeueReusableCellWithIdentifier:indexPath: is that they are different methods! Thus they can behave differently, and they do. This has nothing to do with the indexPath, really; we just need a way to distinguish them.

The New Way

In particular, if you call dequeueReusableCellWithIdentifier:indexPath: , this is a sign that you are using the new iOS 6 register-and-dequeue system. So, if you have failed to register this identifier, you'll get a nice crash and a log message explaining the problem. This method will never return nil; it always returns a cell, either by creating a new one or by reusing one.

The Old Way

On the other hand, plain and simple dequeueReusableCellWithIdentifier: is old and has to be backward compatible. If you haven't registered this identifier, it won't complain: it will just return nil, leaving you high and dry. You'll have to create the cell yourself, as in the bad old days.

EDIT: But see also the answer by @svena! The new way (with indexPath: ) has a second advantage I didn't know about: the cell is correctly sized at the time it is returned to you.


我相信它用于调用tableView:heightForRowAtIndexPath:方法,如果存在的话,允许单元格的大小正确。

链接地址: http://www.djcxy.com/p/66858.html

上一篇: 索引UITableView的节索引标题奇怪的问题是不正确的

下一篇: dequeueReusableCellWithIdentifier:forIndexPath:get的indexPath在哪里使用?