[英]iOS custom grid view for iPad App to show downloaded images
add an object of type collection view then make a proper connection through collection inspector. 添加一个类型为collection view的对象,然后通过collection inspector建立正确的连接。
in .h file 在.h文件中
IBOutlet UICollectionView *myCollection;
in viewdidload 在viewdidload
myCollection.delegate = self;
myCollection.dataSource = self;
and the data source / delegate methods are : 数据源/委托方法是:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection: (NSInteger)section
{
return 15;
}
// The cell that is returned must be retrieved from a call to - dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellID" forIndexPath:indexPath];
cell.backgroundColor=[UIColor greenColor];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout: (UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath * )indexPath
{
return CGSizeMake(100, 100);
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath: (NSIndexPath *)indexPath
{
}
getting this error 得到这个错误
could not dequeue a view of kind: UICollectionElementKindCell with identifier CellID - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
what should i do now… please help me to resolve it.. 我现在该怎么办...请帮助我解决该问题。
You can use UICollectionView
to show grid view in iOS. 您可以使用
UICollectionView
在iOS中显示网格视图。
For Example: 例如:
In HAViewController.h 在HAViewController.h中
#import <UIKit/UIKit.h>
@interface HACollectionCell : UICollectionViewCell
@end
@interface HAViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate>
@end
in HAViewController.m 在HAViewController.m中
#import "HAViewController.h"
@implementation HACollectionCell
@end
@implementation HAViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.width) collectionViewLayout:layout];
collectionView.delegate = self;
collectionView.dataSource = self;
[collectionView registerClass:[HACollectionCell class] forCellWithReuseIdentifier:@"CellID"];
[self.view addSubview:collectionView];
collectionView = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 15;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellID" forIndexPath:indexPath];
cell.backgroundColor=[UIColor greenColor];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(100, 100);
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
}
@end
Thanks! 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.