gpt4 book ai didi

ios - 带有 UIProgressView 的自定义 UICollectionViewCell 在重复使用的单元格上显示 progressView

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:09:54 25 4
gpt4 key购买 nike

我正在对 UICollectionViewCell 进行子类化,因为我想向单元格添加一个 UIImageView 和一个 UIProgressView:

- (id)initWithFrame:(CGRect)frame {

self = [super initWithFrame:frame];
if (self) {

// ImageView
_imageView = [[UIImageView alloc] initWithFrame:self.bounds];
_imageView.layer.borderColor = [UIColor whiteColor].CGColor;
_imageView.layer.borderWidth = 0.7;
[self.contentView addSubview:_imageView];

// Progress View
_progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
_progressView.frame = CGRectMake(5, self.bounds.size.height - 20, self.bounds.size.width - 10, 10);
_progressView.hidden = YES;
[self.contentView addSubview:_progressView];

}
return self;
}

当我触摸一个单元格并调用 collectionView:didSelectItemAtIndexPath: 我设置 cell.progressView.hidden = NO; 并开始下载和更新 progressView.

但是,当我滚动时,该单元格被重用并且 progressView 显示在其他单元格上。我尝试了很多不同的方法来只在正确的单元格上显示它,但我尝试过的任何方法都不起作用。

是否有更好的方法来做到这一点,比如在 prepareForReuse 中做一些事情?

编辑:要求的完整方法

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
PUCViewpointItem *item = [self.items objectAtIndex:indexPath.row];
PUCImageGridCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewCellIdentifier forIndexPath:indexPath];
[cell.imageView setImageWithURL:[NSURL URLWithString:item.imageURLHigh] placeholderImage:[UIImage imageNamed:@"PUCDefaultBackground.png"]];

// See if we have the file already
if (![self.itemPaths objectForKey:item.name]) {
cell.imageView.alpha = 0.4;
} else {
cell.imageView.alpha = 1.0;
}

// See if we are downloading
if (![self.progressItems objectForKey:item.name]) {
cell.progressView.hidden = YES;
} else {
cell.progressView.hidden = NO;
}

return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

PUCViewpointItem *item = [self.items objectAtIndex:indexPath.row];
PUCImageGridCell *cell = (PUCImageGridCell *)[collectionView cellForItemAtIndexPath:indexPath];

// File Path
NSString *path = [self itemPath:item];
if (!path) {

// Set the indexPath we are downloading
[self.progressItems setObject:indexPath forKey:item.name];

Utility *utility = [[Utility alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:item.url]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

NSString *localPath = [[utility localDirectory] stringByAppendingFormat:@"/%@.pdf", item.name];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:localPath append:NO];

[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {

float totalProgress = (float)totalBytesRead/(float)totalBytesExpectedToRead;
if ([[collectionView indexPathForCell:cell] isEqual:indexPath]) {
cell.progressView.alpha = 1.0;
cell.progressView.progress = totalProgress;
}

}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

// This section seems to not get called or updated correctly when the cell
// that is showing the activityView is offscreen
if ([[collectionView indexPathForCell:cell] isEqual:indexPath]) {
[self.itemPaths setObject:localPath forKey:item.name];
[self.progressItems removeObjectForKey:item.name];
cell.imageView.alpha = 1.0;
cell.progressView.alpha = 0.0;
}

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
[self.progressItems removeObjectForKey:item.name];
cell.progressView.alpha = 0.0;
}];

[operation start];

} else {

[self readIssueAtPath:path];

}

}//end

最佳答案

应该存储某个项目是否正在进行下载的信息在某些数据源(或模型)中而不是在单元格( View )中。

然后您可以根据该索引处项目的状态更新数据源委托(delegate)方法 collectionView:cellForItemAtIndexPath: 中的单元格外观路径并显示或隐藏单元格的进度 View 。

ADDED:进度 block 、完成 block 和失败 block 都捕获当前单元格。因此,他们将修改此单元格,即使它已被重新用于不同的索引路径。要解决这个问题,您可以检查单元格的(当前)索引路径是否仍然相等到原始(捕获的)索引路径:

[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {

float totalProgress = (float)totalBytesRead/(float)totalBytesExpectedToRead;
if ([[collectionView indexPathForCell:cell] isEqual:indexPath]) {
cell.progressView.alpha = 1.0;
cell.progressView.progress = totalProgress;
}
}];

完成和失败 block 类似:

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
[self.itemPaths setObject:localPath forKey:item.name];
[self.progressItems removeObjectForKey:item.name];
if ([[collectionView indexPathForCell:cell] isEqual:indexPath]) {
cell.imageView.alpha = 1.0;
cell.progressView.alpha = 0.0;
}

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
[self.progressItems removeObjectForKey:item.name];
if ([[collectionView indexPathForCell:cell] isEqual:indexPath]) {
cell.progressView.alpha = 0.0;
}
}];

关于ios - 带有 UIProgressView 的自定义 UICollectionViewCell 在重复使用的单元格上显示 progressView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16923840/

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