Cell's picture download

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *download;
@property (nonatomic, copy) NSString *icon;

+ (instancetype)appWithDict:(NSDictionary *)dict;
+ (instancetype)appWithDict:(NSDictionary *)dict {

    LMApp *app = [[self alloc] init];
    [app setValuesForKeysWithDictionary:dict];
    return app;
// all application data
@property (nonatomic, strong) NSMutableArray *apps;

/ / Queue for all download operations
@property (nonatomic, strong) NSOperationQueue *queue;

// Store all download operations (url is key, operation object is value)
@property (nonatomic, strong) NSMutableDictionary *operations;

// store all downloaded images
@property (nonatomic, strong) NSMutableDictionary *images;


@implementation LMTableViewController

#pragma mark lazy loading- (NSMutableArray *)apps {

    if (!_apps) {
        // 1. Load plist
        NSString *file = [[NSBundle mainBundle] pathForResource:@"apps" ofType:@"plist"];
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:file];

        // 2. The dictionary is converted to a model stored in an array
        NSMutableArray *appArray = [NSMutableArray array];

        for (NSDictionary *dict in dictArray) {
            LMApp *app = [LMApp appWithDict:dict];
            [appArray addObject:app];
        // 3. Assignment
        self.apps = appArray;
    return _apps;
// other getters are not displayed
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *ID = @"app";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];

    // Remove the modelLMApp *app =self.apps[indexPath.row];

    // Set basic informationcell.textLabel.text = app.name;
    cell.detailTextLabel.text = app.download;

    / / First remove the UIImage corresponding to the image url from the images cache
    UIImage *image = self.images[app.icon];
    if (image) { // Description image has been successfully downloaded (successfully cached)cell.imageView.image = image;
    } else { // Explain that the image has not been downloaded successfully (not cached)
        // show placeholder imagecell.imageView.image = [UIImage imageNamed:@"placeholder"];

        // download image
#warning Download Image[self download:app.icon indexpath:indexPath];
    return cell;
- (void)download:(NSString *)imageUrl indexpath:(NSIndexPath *)indexPath {    
    // Extract the download operation corresponding to the url of the current image (operation object)NSBlockOperation *operation =self.operations[imageUrl];
    if (operation) return;   
    // Create action, download image__weak typeof(self) appVc = self;
    operation = [NSBlockOperation blockOperationWithBlock:^{
        NSURL *url = [NSURL URLWithString:imageUrl];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];       
        // Go back to the main thread[[NSOperationQueue mainQueue] addOperationWithBlock:^{// store the image in the dictionary
            if (image) {
                appVc.images[imageUrl] = image;
            / / Remove the download operation from the dictionary (to prevent operations from increasing, to ensure that the download can be re-downloaded after download failure) [appVc.operations removeObjectForKey:imageUrl];           
            // refresh the table[appVc.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    // Add operation to the queue[self.queue addOperation:operation];
    // added to the dictionary (this sentence is to solve the repeated download)
    self.operations[imageUrl] = operation;
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.   
    // Remove all download operation caches[self.queue cancelAllOperations];
    [self.operations removeAllObjects];
    // Remove all image caches[self.images removeAllObjects];
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    // Pause download[self.queue setSuspended:YES];

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // Reply to download[self.queue setSuspended:NO];