填充时从集合视图中选择一个单元格

Posted

技术标签:

【中文标题】填充时从集合视图中选择一个单元格【英文标题】:Select a cell from collection view while populating 【发布时间】:2014-05-10 05:01:34 【问题描述】:

我正在使用cocos2d 开发一个应用程序,它获取给定用户的所有Facebook 照片,然后将它们填充到CollectionView 中。我通过图形查询获取所有照片URL's 并将它们插入到数组列表中。

[request startWithCompletionHandler:^(FBRequestConnection *connection,
                                          id result,
                                          NSError *error)
        NSArray *resultData = [result objectForKey:@"data"];

        for (int i=0; i<100;i++) 
            NSString *sourceURL = [[resultData objectAtIndex:0] objectForKey:@"source"];
            [_imagesUrlList addObject:sourceURL];
        

        UICollectionViewFlowLayout *collectionFlowLayout = [[UICollectionViewFlowLayout alloc]init];
        [collectionFlowLayout setSectionInset:UIEdgeInsetsMake(5, 7, 5, 7)];
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(_masterView.frame.origin.x, _masterView.frame.origin.y+TOP_MARGIN, _masterView.frame.size.width, _masterView.frame.size.height-TOP_MARGIN) collectionViewLayout:collectionFlowLayout];
        [collectionFlowLayout release];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
        [_collectionView setBackgroundColor:[UIColor colorWithRed:226.f/255.f green:228.f/255.f blue:221.f/255.f alpha:1]];
        [_masterView addSubview:_collectionView];
        [[LoadingView sharedLoadingView] stopTheLoadingActivityView];
        [[LoadingView sharedLoadingView] moveVertically:-30];

        [self loadImagesInView];
    ];

为了测试大量照片,我在索引 0 处插入了图片 url 的 100 倍。 之后,图像将使用来自 Facebook 请求的给定 url 加载到单独的线程上,因为 NSURLConnection 会根据请求阻塞线程。

    -(void)loadImagesInView
    dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
    for (int i=0; i<[_imagesUrlList count]; i++) 
        if (_backed) 
            return;
        
        NSLog(@"out");
        NSString *sourceURL = [_imagesUrlList objectAtIndex:i];
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:sourceURL]];
        NSLog(@"conn %d",i);
        dispatch_async( dispatch_get_main_queue(), ^
            UIImage *image = [UIImage imageWithData:data];
                image = [self imageWithImage:image scaledToSize:CGSizeMake(100, 100)];

                [_imageViewsList addObject:image];


                [_collectionView reloadData];




                NSLog(@"finish %d",i);

            );



    
        );

问题是在视图中加载图像期间我无法选择图片。 didSelect 方法在方法完成之前甚至不会被调用。可能是因为当图像准备好插入时我调用了reloadData。您可以在下面看到选择、取消选择和填充集合视图的方法。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath


    static NSString *cellIdentifier = @"cellIdentifier";

    ImageCell *cell= (ImageCell*)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    UIImage *photoImg = [_imageViewsList objectAtIndex:indexPath.row];
    [cell.imageView setImage:photoImg];
    NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
    if ([_selectedCells containsObject:[NSString stringWithFormat:@"%@",rowNsNum]]) 
        [cell.layer setBorderColor:[[UIColor colorWithRed:255.f/255.f green:255.f/255.f blue:255.f/255.f alpha:0.5] CGColor]];
    
    else 
        [cell.layer setBorderColor:[[UIColor colorWithRed:0.f/255.f green:0.f/255.f blue:0.f/255.f alpha:0.5] CGColor]];
    
    return cell;

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
    ImageCell *imgCell = (ImageCell*)[_collectionView cellForItemAtIndexPath:indexPath];
    if ([_selectedCells containsObject:[NSString stringWithFormat:@"%d",[indexPath row]]]) 
        [_selectedCells removeObject:[NSString stringWithFormat:@"%d",[indexPath row]]];
        [imgCell.layer setBorderColor:[[UIColor colorWithRed:0.f/255.f green:0.f/255.f blue:0.f/255.f alpha:0.5] CGColor]];
    
    else 
        [_selectedCells addObject:[NSString stringWithFormat:@"%d",[indexPath row]]];
        [imgCell.layer setBorderColor:[[UIColor colorWithRed:255.f/255.f green:255.f/255.f blue:255.f/255.f alpha:0.5] CGColor]];
    

-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
    ImageCell *imgCell = (ImageCell*)[_collectionView cellForItemAtIndexPath:indexPath];
    if ([_selectedCells containsObject:[NSString stringWithFormat:@"%d",[indexPath row]]]) 
        [_selectedCells removeObject:[NSString stringWithFormat:@"%d",[indexPath row]]];
        [imgCell.layer setBorderColor:[[UIColor colorWithRed:0.f/255.f green:0.f/255.f blue:0.f/255.f alpha:0.5] CGColor]];
    

【问题讨论】:

【参考方案1】:

放置 [_collectionView reloadData];在 for 循环之外..

 -(void)loadImagesInView
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
for (int i=0; i<[_imagesUrlList count]; i++) 
    if (_backed) 
        return;
    
    NSLog(@"out");
    NSString *sourceURL = [_imagesUrlList objectAtIndex:i];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:sourceURL]];
    NSLog(@"conn %d",i);
    dispatch_async( dispatch_get_main_queue(), ^
        UIImage *image = [UIImage imageWithData:data];
            image = [self imageWithImage:image scaledToSize:CGSizeMake(100, 100)];

            [_imageViewsList addObject:image];


            NSLog(@"finish %d",i);

        );


[_collectionView reloadData];
    );

【讨论】:

是的,但是如果用户有例如 500 张照片,他可能需要等待超过 30 秒才能看到这些照片。 最好的解决方案是使用 AFNetworking。 github.com/AFNetworking/AFNetworking。请按照步骤操作,这将更有帮助。 Step1:下载AFnetworking框架工作。添加 afnetworking 类。 step2: #import "UIImageView+AFNetworking.h" step replace line as ImageCell cell= (ImageCell)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; cell.imageView setImageWithURL:url placeholderImage:kLoadingImage];它可以处理一切。

以上是关于填充时从集合视图中选择一个单元格的主要内容,如果未能解决你的问题,请参考以下文章

单元格未填充在表格内的第二个集合视图中

在 iOS 中具有背景颜色的集合视图单元格上添加图像

触发 UITapGesture 时从单元格的子视图中获取单元格的引用

选择时从 UITableViewCell 中消失的视图

如何根据集合视图中的单元格选择来控制表格视图的内容?

无法在集合视图中选择几个单元格