一、定義我們的CollectViewCell
為云龍等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及云龍網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、云龍網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!// Tiny_CollectionViewCell.h
// Tiny_UICollectionViewController
//
// Created by Tiny on 15-6-16.
// Copyright (c) 2015年 Tiny. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface Tiny_CollectionViewCell : UICollectionViewCell
@property(strong, nonatomic) UIImageView *p_w_picpathView; //圖片定義
@property(strong, nonatomic) UILabel *label; //標(biāo)簽定義
@end
二、實(shí)現(xiàn)我們的CollectViewCell:
//
// Tiny_CollectionViewCell.m
// Tiny_UICollectionViewController
//
// Created by Tiny on 15-6-16.
// Copyright (c) 2015年 Tiny. All rights reserved.
//
#import "Tiny_CollectionViewCell.h"
@implementation Tiny_CollectionViewCell
@synthesize p_w_picpathView;
@synthesize label;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
//初始化圖片
self.p_w_picpathView = [[UIImageView alloc] init];
//定義圖片frame
[self.p_w_picpathView setFrame:CGRectMake(25, 15, 101, 101)];
[self.contentView addSubview:self.p_w_picpathView];
//初始化標(biāo)簽
self.label = [[UILabel alloc] init];
//定義標(biāo)簽frame
[self.label setFrame:CGRectMake(35, 121, 150, 20)];
[self.contentView addSubview:self.label];
}
return self;
}
@end
三、定義我們的控制器ViewController:
//
// Tiny_ViewController.h
// Tiny_UICollectionViewController
//
// Created by Tiny on 15-6-16.
// Copyright (c) 2015年 Tiny. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface Tiny_ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(strong, nonatomic) NSArray *events;
@property(strong, nonatomic) UICollectionView *collectionView;
@end
四、實(shí)現(xiàn)我們的控制器ViewController:
//
// Tiny_ViewController.m
// Tiny_UICollectionViewController
//
// Created by Tiny on 15-6-16.
// Copyright (c) 2015年 Tiny. All rights reserved.
//
#import "Tiny_ViewController.h"
#import "Tiny_CollectionViewCell.h"
@interface Tiny_ViewController ()
@end
@implementation Tiny_ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGRect screenRect = [[UIScreen mainScreen] bounds];
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:@"events" ofType:@"plist"];
//獲取屬性列表文件中的全部數(shù)據(jù)
NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
self.events = array;
UICollectionViewFlowLayout *layout= [[UICollectionViewFlowLayout alloc]init];
self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, screenRect.size.width, screenRect.size.height) collectionViewLayout:layout];
[self.collectionView registerClass:[Tiny_CollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:self.collectionView];
}
- (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
{
Tiny_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
NSDictionary *event = [self.events objectAtIndex:(indexPath.section * 2 + indexPath.row)];
cell.p_w_picpathView.p_w_picpath = [UIImage p_w_picpathNamed:[event objectForKey:@"p_w_picpath"]];
cell.label.text = [event objectForKey:@"name"];
return cell;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *event = [self.events objectAtIndex:(indexPath.section*2 + indexPath.row)];
UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"選擇項(xiàng)目" message:[NSString stringWithFormat:@"你選了%@項(xiàng)目", [event objectForKey:@"name"]] delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
[a show];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(140, 140);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
五、導(dǎo)入events.plist文件(內(nèi)容大概如下)
六、完美運(yùn)行
七、 資源參考 關(guān)老師寫的iOS開發(fā)指南 !
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)站題目:CollectionView純代碼手敲-創(chuàng)新互聯(lián)
本文鏈接:http://muchs.cn/article30/pihpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、網(wǎng)站制作、營銷型網(wǎng)站建設(shè)、云服務(wù)器、企業(yè)建站、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容