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

前端 未结 6 973
别那么骄傲
别那么骄傲 2020-11-30 01:06

Just as the question\'s title mentions: What\'s the difference between \"cellForRowAtIndexPath\" and \"willDisplayCell: forRowAtIndexPath

6条回答
  •  失恋的感觉
    2020-11-30 01:41

    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.

提交回复
热议问题