What is the proper method/accepted standard for loading data asynchronously into table view cells?

前端 未结 4 2097
梦毁少年i
梦毁少年i 2021-02-06 03:04

Many apps such as Tweetbot, Twitterrific, Alien Blue, etc. that display images from an API seem to load them in an asynchronous manner. It seems the initial viewable images are

4条回答
  •  暖寄归人
    2021-02-06 03:39

    There are many solutions to this and you can find a lot of them here on stack overflow.

    The basic idea is you want to load the image either in a block or an NSOperation. I prefer an NSOperation because you can cancel them which is useful when a cell scrolls off screen.

    I recommend a controller to manage your image queue. Your table cell requests the image from the controller and if the controller has the image it returns it immediately. If it doesn't then it returns nil and fetches the image.

    From here there are options. You could use a block to call back when the image is retrieved. I am not a fan of that but it is fairly popular. I prefer to use a NSNotification when the image is received and the cell listens for the NSNotification to populate.

    I have a fairly complicated sample up on github that handles this as well as much much more. It is in my shared repository at http://github.com/ZarraStudios/ZDS_Shared. The main class is calledZSAssetManager.

    Note: This sample code is a few years old so it is not ARC ready and will take some tweaking. It is also probably more complicated than you are looking for as it handles bandwidth detection and reaction as well. However it shows you what a production/high quality application does to handle asynchronous loading of images.

    Enjoy.

提交回复
热议问题