[英]iOS custom grid view for iPad App to show downloaded images
添加一个类型为collection view的对象,然后通过collection inspector建立正确的连接。
在.h文件中
IBOutlet UICollectionView *myCollection;
在viewdidload
myCollection.delegate = self;
myCollection.dataSource = self;
数据源/委托方法是:
- (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
{
}
得到这个错误
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'
我现在该怎么办...请帮助我解决该问题。
您可以使用UICollectionView
在iOS中显示网格视图。
例如:
在HAViewController.h中
#import <UIKit/UIKit.h>
@interface HACollectionCell : UICollectionViewCell
@end
@interface HAViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate>
@end
在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
谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.