如何在Objective-C中的UICollectionView的didSelectItemAtIndexPath委托上显示UIProgressView?

时间:2013-11-21 05:39:06

标签: ios iphone objective-c uicollectionview uiprogressview

我想在用户点击单元格时在集合视图中显示进度视图,并且还希望显示进度该单元格的进度视图

// ...在.h文件中我声明了UIProgressView -

@property(nonatomic,retain)IBOutlet UIProgressView *pv;

// ...然后在.m文件中将其用作

我在 UICollectionView 的方法 cellForItemAtIndexPath 中添加了 UIProgressView ,如下所示;

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"ArticleCell" forIndexPath:indexPath];

    pv = [[UIProgressView alloc]init];
    pv.frame = CGRectMake(45, 270, 230, 50);
    pv.progress = 0.0f;
    pv.progressViewStyle = UIProgressViewStyleDefault;
    pv.hidden = YES;

   [cell.contentView addSubview:pv];

  return cell;

}

然后在 didSelectItemAtIndexPath 中显示进度视图及其进度,如下所示;

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
 //to show progressview in cell..
            UICollectionViewCell *selectedCell = [collView cellForItemAtIndexPath:indexPath];

            NSLog(@"subviews - %@",selectedCell.contentView.subviews);
            pv = (UIProgressView *)[selectedCell.contentView.subviews objectAtIndex:0];
            pv.hidden = NO;

           [self performSelector:@selector(checkDownload) withObject:nil afterDelay:1.0];
}


//to check whether all images are downloaded or not and showing progress accordingly
-(void)checkDownload
{
    NSError *error;

    NSArray* arrArticleImages = [[NSArray alloc]init];

    arrArticleImages = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:imgPath error:&error];
    NSLog(@"arr cnt-%d",arrArticleImages.count );

    if (arrArticleImages.count <= 8)
    {
        float m = (float)arrArticleImages.count / 100;
        float t = pv.progress ;
        float g = m + t;

        pv.progress = g;

        NSLog(@"prgs-%f",g);

        [self performSelector:@selector(checkDownload) withObject:nil afterDelay:4.0];
    }
    else
    {
        pv.progress = 1.0;

        [self insertIntoDatabase];

        [self performSelector:@selector(goToNewsPaperView) withObject:nil afterDelay:2.0];
    }

    arrArticleImages = nil;
}

但现在问题在于,我点击一个单元格,它开始显示进度视图,但只要点击其间的另一个单元格,第一个单元格的进度就会停止显示进度,第二个进度开始显示进度。

如果点击它,我想显示每个单元格的进度视图的进度。

我认为我的做法是错误的。

我无法找到它。

我该怎么做?

请帮帮我。

谢谢..

2 个答案:

答案 0 :(得分:2)

这是因为您在 didSelectItemAtIndexPath 中获得了对您的进度视图的新参考。先前对progrress视图的参考怎么样?

您可以使用键值索引路径 NSDictionary 中添加所有进度视图。 在 checkDownload 中传递您的索引路径,然后通过索引路径 NSDictionary 获取该进度视图并使用它< / p>

//在其他地方初始化NSMutable字典 并在

中添加进度视图
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
        {
            UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"ArticleCell" forIndexPath:indexPath];

            pv = [[UIProgressView alloc]init];

            [dictionary setObject:pv forKey:indexpath];

        }

或者您可以在

中添加进度视图
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
        {
         //to show progressview in cell..
             UICollectionViewCell *selectedCell = [collView cellForItemAtIndexPath:indexPath];

             NSLog(@"subviews - %@",selectedCell.contentView.subviews);
             pv = (UIProgressView *)[selectedCell.contentView.subviews objectAtIndex:0];
             pv.hidden = NO;
             [dictionary setObject:pv forKey:indexpath];

             [self performSelector:@selector(checkDownload) withObject:indexpath afterDelay:1.0];
        }

自定义您使用

检查下载
  -(void)checkDownload:(NSIndexPath*)indexpath
    {
       pv = [dictionary objectForKey:indexpath];
    }

答案 1 :(得分:0)

这是因为您在所有单元格中使用相同的对象pv,您需要执行以下操作:

为每个单元格创建UIProgressView:

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"ArticleCell" forIndexPath:indexPath];

    UIProgressView *pv = [[UIProgressView alloc] init];
    pv.tag = 10;
    .....
}

在didSelectItemAtIndexPath中获取引用并将其传递给下载函数:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
 //to show progressview in cell..
        UICollectionViewCell *selectedCell = [collView cellForItemAtIndexPath:indexPath];

        NSLog(@"subviews - %@",selectedCell.contentView.subviews);
        UIProgressView *pv = (UIProgressView *)[selectedCell viewForTag:10]; 

       // pass the progressView to your checkDownload
       [self performSelector:@selector(checkDownload:) withObject:pv afterDelay:1.0];
}

并且checkDownload将是:

-(void)checkDownload:(UIProgressView *)progressView{
   //use progressView instead of pv
   ....
}