[英]How to add a CollectionView to a ViewController
I want to add a CollectionView inside my ViewController using the same code that I have on a CollectionViewController. 我想使用与CollectionViewController相同的代码在ViewController中添加CollectionView。
CollectionViewController.m 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
I have an NSObject that parses my cell's content, from my database. 我有一个NSObject,可以从数据库中解析单元格的内容。
Product.h 产品h
@interface Product : NSObject
@property (readonly) NSURL *thumbnailImageURL;
- (instancetype)initWithAttributes:(NSDictionary *)attributes;
+ (void)latestProductsWithBlock:(void (^)(NSArray *products, NSError *error))block;
@end
Following a tutorial I fount online, I created a NSObject file ("ProductDataSource") and on my Storyboard I added an Object to my ViewController and linked it to my CollectionView. 在网上查找了一个教程之后,我创建了一个NSObject文件(“ ProductDataSource”),然后在我的情节提要中将一个对象添加到ViewController并将其链接到CollectionView。 I copied the code from my CollectionViewController to ProductDataSource but it's not creating my cells.
我将代码从CollectionViewController复制到ProductDataSource,但是它没有创建单元格。 If I set the numberOfItemsInSection to a number it created the cells but not when I change the code to
return [self.latestProducts count]
. 如果将numberOfItemsInSection设置为数字,则会创建单元格,但更改代码以
return [self.latestProducts count]
时不会创建单元格。 It might have something to do with "loadData" section I have on my CollectionViewController, since ProductDataSource doesn't have a viewDidLoad method. 这可能与我在CollectionViewController上的“ loadData”部分有关,因为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];
}
Any help? 有什么帮助吗? Thanks.
谢谢。
You have to reload the collection view after you get latestProducts
. 获得
latestProducts
之后,您必须重新加载集合视图。 Try to put [self.collectionView reloadData]
after [self hideLoadingView];
尝试将
[self.collectionView reloadData]
放在[self.collectionView reloadData]
[self hideLoadingView];
So if I am understanding correctly, you have an existing UICollectionViewController
called CollectionViewController
and want to reuse the logic in another UIViewController
. 因此,如果我理解正确,那么您已有一个名为
CollectionViewController
UICollectionViewController
并且想在另一个UIViewController
重用该逻辑。
a UICollectionViewController
is essentially a UIViewController
that has a UICollectionView
subview and conforms to the UICollectionViewDataSource
and UICollectionViewDelegate
protocols. UICollectionViewController
本质上是一个UIViewController
,它具有UICollectionView
子视图并符合UICollectionViewDataSource
和UICollectionViewDelegate
协议。
In order to embed a UICollectionView
into your UIViewController
, you need to create Delegate
and DataSource
classes for your CollectionView and assign them in the UIViewController. 为了将
UICollectionView
嵌入到UIViewController
,您需要为CollectionView创建Delegate
和DataSource
类,并将它们分配到UIViewController中。
There are some good code examples here: UICollectionView with UIViewController As Data Source 这里有一些很好的代码示例: 以UIViewController作为数据源的UICollectionView
GitHub Repo with a small single page app using a CollectionView inside a UIViewController: https://github.com/pnavk/CollectionViewSample GitHub Repo带有一个小型单页应用程序,该应用程序使用UIViewController内的CollectionView: https : //github.com/pnavk/CollectionViewSample
Put a breakpoint in your cellForItemAtIndexPath method to ensure that method is being hit. 在cellForItemAtIndexPath方法中放置一个断点,以确保该方法被命中。 If it doesn't get hit, that means you need to set your collection view's datasource.
如果未命中,则意味着您需要设置集合视图的数据源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.