1、案例介紹:以集合形式展示圖片,如圖01ide
圖01atom
2、案例步驟:spa
一、選擇Simple View Aplication,取名cq.38.集合視圖,如圖023d
圖02code
二、Main.storyboard,如圖03blog
圖03圖片
三、events.plist,如圖04,05it
四、CQ38ViewController.hio
#import <UIKit/UIKit.h> @interface CQ38ViewController : UICollectionViewController @property (strong,nonatomic) NSArray *events; @end
五、CQ38ViewController.mevent
#import "CQ38ViewController.h" #import "CQ38CollectionViewCell.h" @interface CQ38ViewController () @end @implementation CQ38ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSBundle *bundle = [NSBundle mainBundle]; NSString *plistPath = [bundle pathForResource:@"events" ofType:@"plist"]; //獲取屬性列表文件中的所有數據 NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath]; self.events = array; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } #pragma mark - UICollectionViewDataSource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return [self.events count] / 2; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 2; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { CQ38CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; NSDictionary *event = [self.events objectAtIndex:(indexPath.section*2 + indexPath.row)]; cell.label.text = [event objectForKey:@"name"]; cell.imageView.image = [UIImage imageNamed:[event objectForKey:@"image"]]; return cell; } #pragma mark - UICollectionViewDelegate - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *event = [self.events objectAtIndex:(indexPath.section*2 + indexPath.row)]; NSLog(@"select event name : %@", [event objectForKey:@"name"]); } @end