[英]Is there a way to run code once a method has finished?
我想在方法完成后开始代码。 我有一个UICollectionView ,它从服务器加载图像数组,然后将这些图像添加到我创建的单元格中的ImageView中,以更好地在这里解释我的代码:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"cellID";
insightCell *myCell = (insightCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
myCell.tag = [[[objectiveGov n_insights] objectAtIndex:indexPath.item]integerValue];
imgTag = ((UICollectionView *)myCell).tag;
//This is the Method I want to run "ObjectiveGov is the NSObject which has my Method inside "arrayImages" which is separate to the view controller this UICollectionView is in.
[objectiveGov arrayImages];
//Possible if statement here, that records when the method above is finished.
myCell.insightImageView.image = [theInsightImage objectAtIndex:indexPath.item];
return myCell;
}
因此,一旦方法[objectiveGov arrayImages];
完成后,我希望它运行myCell.insightImageView.image
。 有办法吗? 我尝试创建一个在方法末尾发布的NSNotificationCenter,但这没有用,因为UICollectionViewCell方法在分配UICollectionView时启动。 有任何想法吗? 谢谢!
编辑 ArrayImages方法代码:
+(void)arrayImages
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"postArrayImages" object:nil];
[[NSUserDefaults standardUserDefaults] synchronize];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/api/insights/get/",URL_ROOT]];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSString *imageString = [NSString stringWithFormat:@"%d",[viewInsightViewController imgTag]];
NSURLRequest *request = [httpClient requestWithMethod:@"GET" path:imageString parameters:nil];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSDictionary *dictionInsight = [JSON valueForKey:@"results"];
NSArray *insightArray = [dictionInsight valueForKey:@"images"];
NSString *fileName = [dictionInsight valueForKey:@"filename"];
if (fileName == (id) [NSNull null])
{
//URL incase server file doesnt contain image.
nineArray = [NSString stringWithFormat:@"http://url.co.uk/images/logo.png"];
}else {
nineArray = [insightArray valueForKey:@"96"];
}
insightURL = [NSURL URLWithString:nineArray];
if (insightURL != nil) {
NSURLRequest *requestImage = [NSURLRequest requestWithURL:insightURL];
AFImageRequestOperation *imageOperation = [AFImageRequestOperation imageRequestOperationWithRequest:requestImage imageProcessingBlock:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
if (requestImage != nil) {
insightImages = image;
[arrayOfImages addObject:insightImages];
}
} failure:nil];
[imageOperation start];
}
对于这种情况,使用块将是完美的。 将您的array images方法更改为类似的方法,以便在完成网络连接后调用该块,以传回已加载的图像。
+ (void)arrayImagesWithCompletionBlock:(void(^)(UIImage *image))completionBlock {
....
if (insightURL != nil) {
NSURLRequest *requestImage = [NSURLRequest requestWithURL:insightURL];
AFImageRequestOperation *imageOperation = [AFImageRequestOperation imageRequestOperationWithRequest:requestImage imageProcessingBlock:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
if (requestImage != nil) {
insightImages = image;
[arrayOfImages addObject:insightImages];
// Call the completion block here once we have the loaded image
completionBlock(image);
}];
} failure:nil];
[imageOperation start];
}
....
}
并在cellForIndexPath中这样称呼它:
[objectiveGov arrayImagesWithCompletionBlock:^(UIImage *loadedImage) {
myCell.insightImageView.image = loadedImage;
}];
最后一件事,就是在实际要执行objectForKey的地方调用valueForKey。 (valueForKey将获得对象的属性值与所需的字典方法的比较,有关详细信息,请使用谷歌“键值编码”)。
也许你可以带一个条件? 完成后,让[objectiveGov arrayImages]返回BOOL = YES。
NSCondition *condition = [NSCondition new];
__block BOOL continueThread = NO;
if ([objectiveGov arrayImages] == YES) {
continueThread = YES;
[condition signal];
[condition unlock];
}
while (!continueThread) [condition wait];
不知道它是否会工作..大声思考一下...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.