Iphone 连接IDFinishLoading后返回collectionView函数
我的问题可能有一个非常明显的答案,但我没有找到。我有这样一个连接:Iphone 连接IDFinishLoading后返回collectionView函数,iphone,ios,objective-c,Iphone,Ios,Objective C,我的问题可能有一个非常明显的答案,但我没有找到。我有这样一个连接: - (void)viewDidLoad { [super viewDidLoad]; NSString *urlstring = [NSString stringWithFormat:@"http://127.0.0.1:8000/image/"]; NSMutableURLRequest *postRequest = [NSMutableURLRequest
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlstring = [NSString stringWithFormat:@"http://127.0.0.1:8000/image/"];
NSMutableURLRequest *postRequest = [NSMutableURLRequest
requestWithURL:[NSURL URLWithString:urlstring]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0];
[postRequest setHTTPMethod:@"POST"];
self.firstConnection = [[NSURLConnection alloc] initWithRequest:postRequest delegate:self];
}
-(void)connectionDidFinishLoading:(NSURLConnection*)connection
{
if (connection == _firstConnection){
// Deal with the data
[self getImages];
[self.collectionView reloadData];
}
-(void)getImages
{
NSError *error;
NSMutableArray* images= [NSJSONSerialization JSONObjectWithData:_data options:0 error:&error];
NSUInteger arrayLength = [images count];
dressURLS = [[NSMutableArray alloc] init];
for (NSInteger i=0;i<arrayLength;i++)
{
NSString *temp = [images[i] objectForKey:@"image"];
[dressURLS addObject:temp];
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *dressImageView = (UIImageView *)[cell viewWithTag:100];
NSString *clothesurl = dressURLS[i]; //i value????
NSString *url =[NSString stringWithFormat:@"http://127.0.0.1:8000/media/%@",clothesurl];
NSURL *imageURL = [NSURL URLWithString:url];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI
dressImageView.image = [UIImage imageWithData:imageData];
});
});
return cell;
}
-(void)viewDidLoad
{
[超级视图下载];
NSString*urlstring=[NSString stringWithFormat:@]http://127.0.0.1:8000/image/"];
NSMutableURLRequest*postRequest=[NSMutableURLRequest
requestWithURL:[NSURL URLWithString:urlstring]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0];
[PostRequestSetHttpMethod:@“POST”];
self.firstConnection=[[NSURLConnection alloc]initWithRequest:postRequest委托:self];
}
-(无效)连接IDFinishLoading:(NSURLConnection*)连接
{
如果(连接==\u第一次连接){
//处理数据
[自我获取图像];
[self.collectionView-reloadData];
}
-(void)获取图像
{
n错误*错误;
NSMUTABLEARRY*images=[NSJSONSerialization JSONObjectWithData:\u数据选项:0错误:&error];
NSU整数数组长度=[图像计数];
dressURLS=[[NSMutableArray alloc]init];
对于(NSInteger i=0;i
“讨论
调用此方法以重新加载集合视图中的所有项目。这将导致集合视图放弃所有当前可见的项目并重新显示。”-Apple您可以第一次和第二次通过重新加载集合视图来检查BOOL的状况和显示图像。这真的不是我的问题。没有什么可以重新加载或重新显示的。也没有类似于[collectionView reload data]的东西
是的,有。@Nikhita,你是什么意思,没有可以重新加载或重新显示的内容?你的问题是,“我想返回collectionView函数,在collectionView中显示我刚收到的URL中的图像。”这不需要您重新加载集合视图吗?这对您有帮助吗?看起来您的NSURLConnection正在获取图像,那么getImages做什么呢?@rdelmar NSURLConnection正在创建连接,但接收数据的NSJSONSerialization正在getImages中进行。因此,在调用getImages后,将您获取的图像添加到whatever数组用于填充集合视图,然后调用reloadData。您尝试过类似的方法吗?@rdelmar我尝试过,但我遇到了各种奇怪的问题,分配了空值。我已经更新了代码。如果有任何帮助,将不胜感激。您是否实现了collectionView:numberOfItemsInSection:?您需要ve该方法返回dressURLs的计数。然后,在cellForItemAtIndexPath中,此行NSString*clothesurl=dressURLs[i]应为NSString*clothesurl=dressURLs[indexath.row]。
-(void)connectionDidFinishLoading:(NSURLConnection*)connection
{
if (connection == _firstConnection){
// Deal with the data
[self getImages];
[collectionView reloaddata]; // or self.collectionView (for property)
}
}