gpt4 book ai didi

ios - 有没有办法在方法完成后运行代码?

转载 作者:行者123 更新时间:2023-11-29 04:18:03 25 4
gpt4 key购买 nike

我想在方法完成后开始代码。我有一个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];
}

最佳答案

使用 block 对于这种情况来说是完美的。将数组图像方法更改为类似这样的方法,以便在 afnetworking 完成时调用传回加载图像的 block 。

+ (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 将获取对象的属性值与您想要的字典方法的比较,请搜索“键值编码”以获取更多信息)。

关于ios - 有没有办法在方法完成后运行代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13397337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com