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.
dequeueReusableCellWithIdentifier: Returns a reusable table-view cell object after locating it by its identifier.
indexPath(for:)Returns an index path that represents the row and section of a specified table-view cell.
Swift version: 5.6. Index paths describe an item's position inside a table view or collection view, storing both its section and its position inside that section. For example, the first row in a table would have section 0, row 0, whereas the eighth row in the fourth section would have section 3, row 7.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With