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

后端 未结 4 1245
慢半拍i
慢半拍i 2020-12-08 00:31

Apple\'s documentation says of the indexPath parameter:

The index path specifying the location of the cell. The data source receives this

4条回答
  •  不知归路
    2020-12-08 01:09

    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.

提交回复
热议问题