【发布时间】:2014-04-24 17:49:07
【问题描述】:
所以我问了几个关于 UICollectionView 的问题。了解它是如何工作的,我正在尝试实现延迟加载以将 15 个图像加载到视图控制器上。我发现很多例子1、2、3...第一个和第三个例子只处理一个操作,第二个例子我认为根本不使用操作,只使用线程。我的问题是可以使用 NSOperation 类和使用/重用操作吗?我读到您无法重新运行操作,但我认为您可以再次初始化它们。这是我的代码:
视图控制器:
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
layout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
[layout setItemSize:CGSizeMake(75, 75)];
self.images = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 230, self.view.frame.size.width, 200) collectionViewLayout:layout];
self.images.delegate = self;
self.images.dataSource = self;
[self.images registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[self.view addSubview:self.images];
self.operation = [[NSOperationQueue alloc]init];
[self.operation addOperationWithBlock:^{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"getgallery.php?user=%@", userId] relativeToURL:[NSURL URLWithString:@"http://www.mywebsite.com/"]];
NSData *data = [NSData dataWithContentsOfURL:url];
//datasource for all images
self.imagesGalleryPaths = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
[self.images reloadData];
//reload collection view to place placeholders
}];
}];
- (void)viewDidAppear:(BOOL)animated{
//get the visible cells right away
visibleCellPaths = [NSArray new];
visibleCellPaths = self.images.indexPathsForVisibleItems;
self.processedImages = [[NSMutableDictionary alloc]initWithCapacity:visibleCellPaths.count];
}
#pragma mark - collection view
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
int i;
if (self.imagesGalleryPaths.count == 0)
i = 25;
else
i = self.imagesGalleryPaths.count;
return i;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [UICollectionViewCell new];
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell.layer.borderWidth = 1;
cell.layer.borderColor = [[UIColor whiteColor]CGColor];
UIImageView *image = [[UIImageView alloc]init];
if (self.imagesGalleryPaths.count != 0) {
if ([visibleCellPaths containsObject:indexPath]) {
[self setUpDownloads:visibleCellPaths];
}
image.image = [UIImage imageNamed:@"default.png"];
image.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
[cell.contentView addSubview:image];
}
return cell;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
visibleCellPaths = [NSArray new];
visibleCellPaths = self.images.indexPathsForVisibleItems;
[self setUpDownloads:visibleCellPaths];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
visibleCellPaths = [NSArray new];
visibleCellPaths = self.images.indexPathsForVisibleItems;
[self setUpDownloads:visibleCellPaths];
}
- (void)setUpDownloads:(NSArray *)visiblePaths{
//I want to pass the visiblePaths to the NSOperation class if visible cells changed
GalleryOps *gallery = [[GalleryOps alloc]init];
//I will use a dictionary to keep track of which indexPaths are being downloaded...
//...so there are no duplicate downloads
}
GalleryOps.m
@implementation GalleryOps
- (void)main{
//would I initialize all the operations here and perform them?
}
显示空的 GalleryOps 类几乎没有意义,因为我不知道如何使用多个操作对其进行初始化。我知道我必须重写 main 方法,一旦我从 URL 获取图像数据,我需要更新 UI,为此我需要一个委托方法......另一件事我还不知道该怎么做但是有很多例子可以说明这一点。我最大的问题是如何将可见单元格传递给这个类并运行多个操作?当新的可见单元格出现时,我会检查一下要取消哪些,保留哪些。这里有什么建议吗?提前致谢!
【问题讨论】:
-
为什么每次下载都没有操作实例?然后你可以控制有多少与队列同时运行。
-
你能举个例子吗?在 NSOperation 类中还是在视图控制器中?
-
你已经在做
addOperationWithBlock:,将一个操作实例添加到队列中——你能再做一次同样的事情吗... -
@Rob,我知道有很多图书馆,但作为学习过程的一部分,我想先了解基础知识的工作原理,然后再让我的生活更轻松。
-
@Wain,我认为你是对的。我可以通过队列块操作循环遍历可见单元格,同时检查作为参数 indexPath 传递的内容,比较已加载的图像,并一一插入图像。取消对 viewWillDisappear 的所有操作。如果不可能进行多个 NSOperations,现在可能是最好的方法。
标签: ios objective-c nsoperation