iOS UICollectionView 的简单显示
2016-03-28 22:53
411 查看
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic ,strong) UICollectionView *collection;
@property (nonatomic ,strong) NSMutableArray *arrImg;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.arrImg = [NSMutableArray array];
for (int i = 0; i < 47; i++) {
[self.arrImg addObject:[NSString stringWithFormat:@"%d.jpg",i]];
}
//
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
self.collection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
//self.collection.backgroundColor = [UIColor redColor];
self.collection.dataSource =self;
self.collection.delegate = self;
[self.view addSubview:self.collection];
[self.collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Collection"];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.arrImg.count;
}
//-( NSInteger )numberOfSectionsInCollectionView:( UICollectionView *)collectionView
//
//{
//
// return 1 ;
//
//}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Collection" forIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:self.arrImg[indexPath.row]]];
return cell;
}
-( void )collectionView:( UICollectionView *)collectionView didSelectItemAtIndexPath:( NSIndexPath *)indexPath
{
UICollectionViewCell * cell = ( UICollectionViewCell *)[collectionView cellForItemAtIndexPath :indexPath];
cell. backgroundColor = [ UIColor colorWithRed :(( arc4random ()% 255 )/ 255.0 ) green :(( arc4random ()% 255 )/ 255.0 ) blue :(( arc4random ()% 255 )/ 255.0 ) alpha : 1.0f ];
}
-( BOOL )collectionView:( UICollectionView *)collectionView shouldSelectItemAtIndexPath:( NSIndexPath *)indexPath
{
return YES ;
}
- ( CGSize )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:( NSIndexPath *)indexPath
{
return CGSizeMake ( 90 , 90 );
}
-( UIEdgeInsets )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:( NSInteger )section
{
return UIEdgeInsetsMake ( 10 , 10 , 10 , 10 );
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
运行结果如下:
@end
@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic ,strong) UICollectionView *collection;
@property (nonatomic ,strong) NSMutableArray *arrImg;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.arrImg = [NSMutableArray array];
for (int i = 0; i < 47; i++) {
[self.arrImg addObject:[NSString stringWithFormat:@"%d.jpg",i]];
}
//
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
self.collection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
//self.collection.backgroundColor = [UIColor redColor];
self.collection.dataSource =self;
self.collection.delegate = self;
[self.view addSubview:self.collection];
[self.collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Collection"];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.arrImg.count;
}
//-( NSInteger )numberOfSectionsInCollectionView:( UICollectionView *)collectionView
//
//{
//
// return 1 ;
//
//}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Collection" forIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:self.arrImg[indexPath.row]]];
return cell;
}
-( void )collectionView:( UICollectionView *)collectionView didSelectItemAtIndexPath:( NSIndexPath *)indexPath
{
UICollectionViewCell * cell = ( UICollectionViewCell *)[collectionView cellForItemAtIndexPath :indexPath];
cell. backgroundColor = [ UIColor colorWithRed :(( arc4random ()% 255 )/ 255.0 ) green :(( arc4random ()% 255 )/ 255.0 ) blue :(( arc4random ()% 255 )/ 255.0 ) alpha : 1.0f ];
}
-( BOOL )collectionView:( UICollectionView *)collectionView shouldSelectItemAtIndexPath:( NSIndexPath *)indexPath
{
return YES ;
}
- ( CGSize )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:( NSIndexPath *)indexPath
{
return CGSizeMake ( 90 , 90 );
}
-( UIEdgeInsets )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:( NSInteger )section
{
return UIEdgeInsetsMake ( 10 , 10 , 10 , 10 );
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
运行结果如下:
@end
相关文章推荐
- CodeForces 3D Least Cost Bracket Sequence
- Errors running builder "Android Package Builder" on project "***"minvor version 52
- UI进阶——第三方的使用
- UVa 11063 - B2-Sequence
- UIView的基本属性及ANimation
- STL中vector、list、deque和map的区别
- uva10128(Queue)
- iOS之UITableView的上拉刷新
- OC中的Values——and——Collections
- 303. Range Sum Query - Immutable
- easyui-combobox实现级联查询
- uestc1134男神的约会【状压dp】
- Fluent NHibernate other example
- iOS - UITableViewCell reload 数据刷新method
- [leetcode] 53. Maximum Subarray
- 《leetCode》:Repeated DNA Sequences
- Atitit.Gui控件and面板----数据库区-mssql 2008 权限 配置 报表查看成员
- UVA 540 —— Team Queue
- java 乱码详解_jsp中pageEncoding、charset=UTF -8"、request.setCharacterEncoding("UTF-8")
- 理解 Android Build 系统