Asynchronously set images in tableview

前端 未结 4 1149
鱼传尺愫
鱼传尺愫 2020-12-22 12:27

I have a TableView using custom cells. I initially was setting grabbing an image from a URL in the cellForRowAtIndexPath method

- (         


        
4条回答
  •  失恋的感觉
    2020-12-22 12:48

    Step 1: Have a cache containing images. Either just in memory, better on disk.

    Step 2: When you need an image, call a method which either returns an image from the cache, or returns a default image and starts a download.

    Step 3: When a download finishes, add the image to the cache. Then find out which rows need the image. Reload all the rows that reload the image.

    The download should be done asynchronously using GCD. I would really recommend that you add the download code into a separate, reusable method so that you can handle download errors. Even if you don't do it now, you will do it later.

提交回复
热议问题