why do programmers use configureCell:atIndexPath: method to config the tableView Cell

前端 未结 4 1840
攒了一身酷
攒了一身酷 2021-01-30 22:22

Well till a couple of days back I use to code everything for UITableViewCell in

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAt         


        
4条回答
  •  鱼传尺愫
    2021-01-30 23:06

    It's done because you may want to update cells when they're already on the screen. Rather than completely refreshing the cell, you can simply fetch the existing cell from the table view and run it through configureCell:atIndexPath:. If the method is correctly implemented, this will update all the data in the cell without having UITableView remove the old cell, dequeue or allocate the new cell, and put it on screen.


    For historical interest:

    As far as I know, I'm the guy who's responsible for configureCell:atIndexPath:. I'm sure other people have come up with the same idea, but I believe the snippet of code that popularized it was originally written by me. It was then spread by Apple and became a convention.

    The early versions of NSFetchedResultsControllerDelegate had a controllerDidChangeContent: method, but no controllerWillChangeContent: call, which meant there was no opportunity to call -[UITableView beginUpdates] before changing the contents of the table view.

    I filed Radar #6708453 asking for them to add this delegate method, and included some example code to show them what I wanted to do. That code had the actual cell updating logic in a refreshCell:atIndexPath: call, so that it could be called from both tableView:cellForRowAtIndexPath: and controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:.

    When the next beta seed came out, I found that the iOS engineering team not only added the method I suggested, but copied the sample code from my bug report into the NSFetchedResultsControllerDelegate documentation, although they wisely changed the name to the less confusing configureCell:atIndexPath:.

    I actually started a new project today with the Master/Detail iOS Core Data template, and noticed that my code—and the configureCell:atIndexPath: method with it—was in the template. I ran a quick Google search to see if it had become a common convention. It seems that it has. I'm rather proud of what that little chunk of code has made of itself!

提交回复
热议问题