ios table view cell load images in the order of row index

不想你离开。 提交于 2019-12-13 07:17:39

问题


Configuring cells in a table view, I use the

-(void)setImageWithURLRequest:(NSURLRequest *)urlRequest placeholderImage:(UIImage *)

method from AFNetworking to load cell images ( put it in cellForRowAtIndexPath ) from web

that users won't get stuck scrolling while it's loading.

----------

the question is that every image show up in random order

because this method sends asynchronous request,

yet I want them to be displayed one after another,

such as the image in row 1 cell must show up after image in row 0 cell is shown, and so on

it's like the loading could be asynchronous( and it must be)...

but the display should be synchronous (in order!)

is there an easy way to do this?

thanks in advance


回答1:


Store the name of the images in an array and access it in cellForItemAtIndexPath() method of the UITableView. By this way you will get the proper order of the images. You should not display images one after another because of different sizes of the images(for example - if the image size is in megabytes, it would take a lot of time to load, that time can be used to load another images)

NSURL *photoURL = [NSURL URLWithString:[[array objectAtIndex:indexPath.row] valueForKey:@"Image"]];
[cell.imgMyPhoto setImageWithURL:photoURL placeholderImage:nil usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];



回答2:


If you want to show them one after another, you can use this code to set execution order to FIFO which will give u desired output.

[[[SDWebImageManager sharedManager] imageDownloader] setExecutionOrder:SDWebImageDownloaderFIFOExecutionOrder];

You can use this method as mentioned above by Utsav Parikh

NSURL *photoURL = [NSURL URLWithString:[[array objectAtIndex:indexPath.row] valueForKey:@"Image"]];
[cell.imgLogo setImageWithURL :photoURL placeholderImage : kPhotoPlaceholder usingActivityIndicatorStyle : UIActivityIndicatorViewStyleGray];

but dont forget to import the header file

#import "UIImageView+UIActivityIndicatorForSDWebImage.h"


来源:https://stackoverflow.com/questions/28021281/ios-table-view-cell-load-images-in-the-order-of-row-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!