0

我正在使用 xcode 5 和 ios 7。当我滚动到表格末尾时,从服务器异步加载数据(图片 50-100kb)。尝试 10 次后出现内存警告.... 使用GCD异步下载。如何解决问题又不失滚动内容的流畅度?与 GCD 一起使用 ARC?

细胞创建:

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellID = [NSString stringWithFormat:@"Cell%i",indexPath.section];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    /*
    First section simple text cell
    Second section hard cells
    */
    if(cell == nil)
    {
        switch (indexPath.section)
        {
            case 0:
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                break;
            }
            case 1:
            {
                cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                break;
            }            
            default:
            {
                break;
            }
        }

    }

    if (indexPath.section == 1)
    {
        NSInteger index = indexPath.row;
        ModelInfo* model = [arrayModelInfo objectAtIndex:index];
        [(CustomCell*)cell setContent:model];//Update subview in cell with parametrs model
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

自定义单元集内容

[self.CustomConentView.imageView setImage:model.image];
[self.CustomConentView.label setText:model.name]; 

下载:

NSInteger serverOffset = 0;
.....
dispatch_async(queueDownloadData, ^{
        [self backgroundDownloadRecometedProducts];
    });    
.....

- (void) backgroundDownload
{
    Network* network = [[LMNetwork alloc] init];
    [network getRecomendedProductOffset:serverOffset
                                Success:^(id JSON) {
                                    dispatch_async(queueDownloadData, ^{
                                        recomentedProductsOffset += 10;
                                        [self parse:JSON];
                                    });

                                }];
}

- (void) parse: (id) JSON
{
    NSArray* result = [JSON objectForKey:@"result"];
    for (NSDictionary* parametrs in result)
    {
        ModelInfo* model = [[ModelInfo alloc] initWithDictionary:parametrs];

        [arrayModelInfo addObject:model];

        dispatch_async(self.queueDownloadImages, ^{
            [self downloadImageForName:model.thumbnail Tag:[arrayModelInfo indexOfObject:model]];
        });
    }

    dispatch_async(dispatch_get_main_queue(), ^{
        [tableViewContent reloadData];
    });
}

- (void) downloadImageForName: (NSString*) imageName Tag: (NSInteger) tag
{
    Network* network = [[Network alloc] init];
    [network downloadImageWithName:imageName
                          ImageTag:tag
                           Success:^(UIImage *image, NSInteger tag) {
                               ModelInfo* model = [arrayModelInfo objectAtIndex:tag];
                               model.image = image;
                               dispatch_async(dispatch_get_main_queue(), ^{
                                   [tableViewContent reloadData];
                               });
                           }];
}

在服务器的网络类发布请求中,我使用了 AFNetworking

4

0 回答 0