iOS UITableView: what’s the different between “cellForRowAtIndexPath” and “willDisplayCell: forRowAtIndexPath:”

You are right, cell configuration can (in theory) be done in both methods.

However, almost all UITableView have a data source which implements cellForRowAtIndexPath: (it is a required method in the protocol). On the other hand, the willDisplayCell:forRowAtIndexPath: (which is a method of the delegate, not the data source) is optional.

As configuring a cell is usually dependent on the data you want to show, cellForRowAtIndexPath: is by far the most common place to do cell configuration. (I can’t even remember using willDisplayCell:forRowAtIndexPath:).

There’s one notable exception: when you are using a storyboard and static cells (instead of cell prototypes), you can’t do anything useful in cellForRowAtIndexPath: (because dequeueReusableCellWithIdentifier: returns nil), so you have to do configuration in willDisplayCell:forRowAtIndexPath:, viewWillAppear: or other methods.

@NSDeveloper: you’re right. Thanks for the hint.

Leave a Comment