Upvote:36
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.
Upvote:40
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.
More Answer related to the Same Query
- WHERE DOES THE INDEXPATH OF DEQUEUEREUSABLECELLWITHIDENTIFIER:FORINDEXPATH: GET USED? IN IOS
- APP TRACKING TRANSPARENCY HOW DOES EFFECT APPS SHOWING ADS? - IDFA IOS14 IN IOS
- ALAMOFIRE DOES NOT RESPECT TIMEOUT INTERVAL IN IOS
- BOOTSTRAP 3 ACCORDION COLLAPSE DOES NOT WORK ON IPHONE IN IOS
- ARGUMENT OF '#SELECTOR' DOES NOT REFER TO AN '@OBJC' METHOD, PROPERTY, OR INITIALIZER IN IOS
- WKWEBVIEW DOES LOAD RESOURCES FROM LOCAL DOCUMENT FOLDER IN IOS
- WHAT DOES NSURLCONNECTION'S ERROR CODE "-1009" MEAN? IN IOS
Upvote:2
I believe it is used to call the tableView:heightForRowAtIndexPath:
method, if one exists, allowing the cell to be correctly sized.
Upvote:-2
I always thought that UIKit would round the corners of the top and bottom cells in a grouped table view when the UITableViewDelegate
method:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
is called. However, knowing the index path isn't much use unless the cell also knows how many rows are in the same section so it can work out if it's the last cell or not.
I assume that the addition of the index path to the method dequeueReusableCellWithIdentifier:
is to improve performance perhaps as @jrturton suggested with different reuse pools or simply to determine the position of a cell in grouped sections.
As far as I can remember from the WWDC videos, there were a few additional methods added in iOS 6 to support reordering, insertion and deletion of cells so perhaps this also comes into factor here?
Credit Goes to: stackoverflow.com
Related question with same questions but different answers
- UNIT TESTING EXAMPLE WITH OCUNIT IN IOS
- TYPE "MYVIEWCONTROLLER" DOES NOT CONFORM TO PROTOCOL UIPICKERDATASOURCE IN SWIFT IN IOS
- HOW DOES ONE PRINT ALL WKWEBVIEW ON AND OFFSCREEN CONTENT OSX AND IOS IN IOS
- WHY DOES BOTTOM TABLE CELL THAT HAS CENTERED-TEXT GET CUT OFF WHEN DISPLAYED AS PDF IN IOS? IN IOS
- THE BUNDLE'S INFO.PLIST DOES NOT CONTAIN A CFBUNDLEVERSION KEY OR ITS VALUE IS NOT A STRING IN IOS
- WHY DOES GOOGLE CHROME EMULATOR SHOW IPHONE 6 AT 375X667 RESOLUTION? IN IOS
- KVO - HOW TO CHECK IF AN OBJECT IS AN OBSERVER? IN IOS
- UIIMAGE ON SWIFT CAN'T CHECK FOR NIL IN IOS
- HOW TO PREVENT PULL-DOWN-TO-REFRESH OF MOBILE CHROME IN IOS
- UICOLLECTIONVIEW: ONE ROW OR COLUMN IN IOS
- JENKINS - FAILED: NO TEST REPORT FILES WERE FOUND. CONFIGURATION ERROR? IN IOS
- HOW DO YOU MAKE AN UIPICKERVIEW COMPONENT WRAP AROUND? IN IOS
- DOES IPHONE 6 / 6 PLUS SIMULATOR SUPPORTS CHANGING OF DISPLAY ZOOM MODE? IN IOS
- DETECT "ALLOW NOTIFICATIONS" IS ON/OFF FOR IOS8 IN IOS
- UIPAGEVIEWCONTROLLER CLASS CRASHING ON SETTING MORE THAN ONE VIEW CONTROLLERS IN IOS
- WHICH IDE FOR PHONEGAP? IS ECLIPSE ENOUGH? IN IOS
- HOW DO YOU CHANGE THE COLOR OF A UIFONT? IN IOS
- HOW TO CONVERT .DAE TO .SCN FILES IN SCENEKIT IN IOS
- LOCATION SERVICES IN MOBILE SAFARI: DON'T ALLOW IS SAVED...FOREVER? EVEN WITH RESET IN IOS
- STATIC LIBRARIES IN XCODE 4 IN IOS
- HOW CAN I ENABLE/DISABLE SECTION HEADERS IN UICOLLECTIONVIEW PROGRAMMATICALLY? IN IOS
- CHANGE PAID APP TO FREE BUT KNOW IF USER PREVIOUSLY PURCHASED IT IN IOS
- RTCREPORTING & PANCAKE.APPLE.COM ERRORS IN IOS
- USING JSONENCODER TO ENCODE A VARIABLE WITH CODABLE AS TYPE IN IOS
- SCALE FIT MOBILE WEB CONTENT USING VIEWPORT META TAG IN IOS
- IOS BUILD NUMBER IN CORDOVA APP IN IOS
- ON IOS 7, PUSHING A CONTROLLER WITH A TOOLBAR LEAVES A GAP OF UNUSABLE SPACE IF IT'S ULTIMATELY CONTAINED WITHIN A TAB BAR CONTROLLER IN IOS
- ADDING CUSTOM FONTS TO IOS APP FINDING THEIR REAL NAMES IN IOS
- CENTER MULTI-LINE TEXT ON UIBUTTON USING IB IN IOS
- WHAT'S WRONG HERE: INSTANCE MEMBER CANNOT BE USED ON TYPE IN IOS
- TABLEVIEW:CANEDITROWATINDEXPATH: CRASH WHEN POPPING VIEWCONTROLLER IN IOS
- AES ENCRYPTION IN SWIFT IN IOS
- CONVERT NSDATA LENGTH FROM BYTES TO MEGS IN IOS
- HOW TO USE COCOAPODS WITH PLAYGROUND? IN IOS
- ITUNES CONNECT: CAN'T SEE BUILD IN IOS
- IOS - HOW CAN I PRELOAD THE KEYBOARD? IN IOS
- HOW TO USE DRAWINRECT:WITHATTRIBUTES: INSTEAD OF DRAWATPOINT:FORWIDTH:WITHFONT:FONTSIZE:LINEBREAKMODE:BASELINEADJUSTMENT: IN IOS 7 IN IOS
- RECEIVING PUSH NOTIFICATIONS WHILE IN BACKGROUND IN IOS
- AUTOLAYOUT - POSITION BY PERCENTAGE IN IOS
- IOS SAMPLE PROJECTS TO LEARN FROM IN IOS
- SWIFT 2.0 MINIMUM SYSTEM VERSION REQUIREMENT (DEPLOYMENT TARGET) IN IOS
- UITABLEVIEW BOUNCING BACK TO THE TOP OF A SECTION WHEN CALLING RELOADROWSATINDEXPATHS IN IOS
- REACT NATIVE: NO PROPTYPE FOR NATIVE PROP RCTVIEW.MAXHEIGHT IN IOS
- "MESSAGE REPLY TOOK TOO LONG." - WATCH CONNECTIVITY ISSUES WITH WATCH OS 3 IN IOS
- REACT NATIVE 0.64 WON'T BUILD IOS APP AFTER UPDATING XCODE TO 12.5 AND IOS TO 14.5 IN IOS
- UIVIEW ANIMATIONS WITH AUTOREVERSE IN IOS
- TABLEVIEW RELOADDATA VS. BEGINUPDATES & ENDUPDATES IN IOS
- IS THE WEAKSELF/STRONGSELF DANCE REALLY NECESSARY WHEN REFERENCING SELF INSIDE A NON-RETAINED COMPLETION CALLED FROM A UIVIEWCONTROLLER? IN IOS
- IOS 8: WEB APP STATUS BAR POSITION AND RESIZING PROBLEMS IN IOS
- IS XCODE 5 ASSET CATALOG BACKWARDS COMPATIBLE WITH PRE-IOS 7? IN IOS
- TABLEVIEW SECTION HEADERS DISAPPEAR SWIFT IN IOS