我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

CollectionView纯代码手敲-创新互联

一、定义我们的CollectViewCell

为云龙等地区用户提供了全套网页设计制作服务,及云龙网站建设行业解决方案。主营业务为成都网站设计、成都网站制作、云龙网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

// Tiny_CollectionViewCell.h

// Tiny_UICollectionViewController

//

// Created by Tiny on 15-6-16.

// Copyright (c) 2015年 Tiny. All rights reserved.

//

#import

@interface Tiny_CollectionViewCell : UICollectionViewCell

@property(strong, nonatomic) UIImageView *p_w_picpathView; //图片定义

@property(strong, nonatomic) UILabel *label; //标签定义

@end

二、实现我们的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];

    //初始化标签

    self.label = [[UILabel alloc] init];

    //定义标签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

@interface Tiny_ViewController : UIViewController

@property(strong, nonatomic) NSArray *events;

@property(strong, nonatomic) UICollectionView *collectionView;

@end

四、实现我们的控制器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"];

  //获取属性列表文件中的全部数据

  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:@"选择项目" message:[NSString stringWithFormat:@"你选了%@项目", [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

五、导入events.plist文件(内容大概如下)

    CollectionView纯代码手敲

六、完美运行

    CollectionView纯代码手敲

七、 资源参考 关老师写的iOS开发指南 !

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站标题:CollectionView纯代码手敲-创新互联
网站链接:http://shouzuofang.com/article/pihpo.html

其他资讯