在上一个方法运行中重新加载tableView

时间:2014-05-03 19:32:51

标签: ios uitableview nsnotification

我有下载一张图片时收到通知的方法。我可以下载很多对象,我想更新tableView然后我有下载图像的最后通知。如何实现它的最佳方法?

- (void)requestDownloadAvatarWithImageString:(NSString *)imageString completion:(void (^)(AFHTTPRequestOperation *operation, UIImage *image, NSError *error))completion __attribute__((nonnull(2)))
{
NSParameterAssert(completion);

NSString *urlString = imageString;
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];

AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];

[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    completion(operation, responseObject, nil);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    DDLogError(@"FAIL download image: %@",error);
}];

[requestOperation start];
}

1 个答案:

答案 0 :(得分:0)

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_semaphore_t semaphore = dispatch_semaphore_create(NUMBER_OF_RETURNED_OBJECTS);
dispatch_async(queue, ^{
    for (i = 0; i<NUMBER_OF_RETURNED_OBJECTS; i++)
    {
        dispatch_async(queue, ^{

            fetchObject(i); // it's specific to your application

            dispatch_semaphore_signal(semaphore);
        }
    }
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}   

[your_table_view reloadData];

或者只是获取数组中的所有图像路径,然后使用SDWebImage下载它们

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];


NSString *url = [NSString stringWithString, imageArray[indexPath.row]];

[cell.cell_image setImageWithURL:[NSURL URLWithString: url] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

}