我想使用与CollectionViewController上相同的代码在ViewController中添加一个CollectionView。
CollectionViewController.m
@interface StoreViewController ()
@property (readwrite, nonatomic, strong) NSArray *latestProducts;
@end
@implementation StoreViewController
- (void)setLatestProducts:(NSArray *)latestProducts {
_latestProducts = latestProducts;
[self.collectionView reloadData];
}
- (Product *)releaseForIndexPath:(NSIndexPath *)indexPath {
return [self.latestProducts objectAtIndex:indexPath.row];
}
- (void)loadData:(id)sender {
[self showLoadingView];
[Product latestProductsWithBlock:^(NSArray *products, NSError *error) {
self.latestProducts = products;
dispatch_async(dispatch_get_main_queue(), ^{
[self hideLoadingView];
});
if (error) {
[[[UIAlertView alloc] initWithTitle:[error localizedDescription] message:[error localizedFailureReason] delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles:nil, nil] show];
}
}];
}
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = NSLocalizedString(@"Deadstock", nil);
[self.collectionView registerClass:[ProductCell class] forCellWithReuseIdentifier:@"ProductCell"];
[self loadData:nil];
}
#pragma mark - UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.latestProducts count];
}
#pragma mark - Collection View Cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"productCell";
ProductCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.product = [self releaseForIndexPath:indexPath];
return cell;
}
@end
ProductCell.m
@implementation ProductCell
- (void)setProduct:(Product *)product {
_product = product;
dispatch_async(dispatch_get_main_queue(), ^{
[self.image setImageWithURL:self.product.thumbnailImageURL];
});
self.image.clipsToBounds = YES;
}
@end
我有一个NSObject,可以从我的数据库中解析我单元格的内容。
Product.h
@interface Product : NSObject
@property (readonly) NSURL *thumbnailImageURL;
- (instancetype)initWithAttributes:(NSDictionary *)attributes;
+ (void)latestProductsWithBlock:(void (^)(NSArray *products, NSError *error))block;
@end
按照我在线编写的教程,我创建了一个NSObject文件(" ProductDataSource"),在我的故事板上,我向ViewController添加了一个Object,并将其链接到我的CollectionView。我将代码从我的CollectionViewController复制到ProductDataSource,但它没有创建我的单元格。如果我将numberOfItemsInSection设置为一个数字,则会创建单元格,但不会将代码更改为return [self.latestProducts count]
。它可能与" loadData"有关。我在CollectionViewController上的部分,因为ProductDataSource没有viewDidLoad方法。
- (void)loadData:(id)sender {
[self showLoadingView];
[Product latestProductsWithBlock:^(NSArray *products, NSError *error) {
self.latestProducts = products;
dispatch_async(dispatch_get_main_queue(), ^{
[self hideLoadingView];
});
if (error) {
[[[UIAlertView alloc] initWithTitle:[error localizedDescription] message:[error localizedFailureReason] delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles:nil, nil] show];
}
}];
}
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = NSLocalizedString(@"Deadstock", nil);
[self.collectionView registerClass:[ProductCell class] forCellWithReuseIdentifier:@"ProductCell"];
[self loadData:nil];
}
有任何帮助吗?感谢。
答案 0 :(得分:0)
获得latestProducts
后,您必须重新加载集合视图。尝试将[self.collectionView reloadData]
放在[self hideLoadingView];
答案 1 :(得分:0)
因此,如果我理解正确,您就会有一个名为UICollectionViewController
的现有CollectionViewController
,并希望在另一个UIViewController
中重复使用该逻辑。
UICollectionViewController
基本上是UIViewController
,其UICollectionView
子视图符合UICollectionViewDataSource
和UICollectionViewDelegate
协议。
要在UICollectionView
中嵌入UIViewController
,您需要为CollectionView创建Delegate
和DataSource
类,并在UIViewController中分配它们。
这里有一些很好的代码示例:UICollectionView with UIViewController As Data Source
使用UIViewController内部的CollectionView的小型单页应用程序GitHub Repo: https://github.com/pnavk/CollectionViewSample
答案 2 :(得分:0)
在您的cellForItemAtIndexPath方法中放置一个断点,以确保该方法被命中。如果它没有被命中,那意味着你需要设置你的集合视图的数据源。