[英]UICollectionView didselectitematindexpath delegate?
[英]how to display UIProgressView on didSelectItemAtIndexPath delegate of UICollectionView in Objective-C ?
我想在用戶單擊單元格時在集合視圖中顯示進度視圖 ,並且還想顯示該單元格進度視圖的進度。
// ...在.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;
}
但是現在的問題是,我單擊一個單元格開始顯示帶有進度的進度視圖,但是一旦單擊之間的另一個單元格,第一個單元格的進度就會停止顯示進度,而第二個進度會開始顯示進度。
我想顯示每個單元格的progressview的進度(如果單擊)。
我認為我的方法是錯誤的。
我無法找到答案。
我怎樣才能做到這一點??
請幫我。
謝謝..
這是因為您在didSelectItemAtIndexPath上獲得了進度視圖的新引用 。 以前對progrress視圖的看法怎么樣?
您可以在NSDictionary中使用鍵值索引路徑添加所有進度視圖 。 然后在checkDownload中傳遞您的索引路徑,然后從NSDictionary獲取具有傳遞的索引路徑的進度視圖並使用它
//在其他地方初始化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];
}
這是因為您在所有單元格中都使用相同的對象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
....
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.