UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView 和 UITableViewController 类

1.定义Cell

@interface CollectionViewCell : UICollectionViewCell

@property (strong, nonatomic) UILabel * titleLabel;

@property (strong, nonatomic) UIButton * deleButton;

@property (strong, nonatomic) UIButton * colorButton;

@property (strong, nonatomic) UILabel * numberLabel;

@end

@implementation CollectionViewCell

- (id)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

[self initializeUserInterface];

self.contentView.layer.borderWidth = 1.0f;

self.contentView.layer.borderColor = [UIColor redColor].CGColor;

}

return self;

}

- (void) initializeUserInterface {

UIView *bgView=[[UIView alloc]initWithFrame:

CGRectMake(2, 2, CGRectGetWidth(self.bounds)-35, CGRectGetHeight(self.bounds)-5)];

bgView.layer.cornerRadius=5;

bgView.tag=13;

// bgView.layer.borderColor=GARY2_COLOR.CGColor;

bgView.layer.borderWidth=1;

[self addSubview:bgView];

self.deleButton=[UIButton buttonWithType:UIButtonTypeSystem];

self.deleButton.frame=CGRectMake(CGRectGetWidth(self.bounds)-27, 2, 27, 27);

// deleBtn.backgroundColor=[UIColor orangeColor];

[self.deleButton setBackgroundImage:GETIMAGE(@"5-1处方用药_常用药_03.png") forState:UIControlStateNormal];

// self.deleButton.tag=11;

self.deleButton.hidden=YES;

[self.contentView addSubview:self.deleButton];

_titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(5, 0, CGRectGetWidth(bgView.bounds)-5, 30)];

_titleLabel.font=[UIFont boldSystemFontOfSize:20];

[self.contentView addSubview:_titleLabel];

.......

}

//-----------------

@interface ViewController : UICollectionViewController

@end

@interface ViewController ()<UICollectionViewDelegateFlowLayout,UIAlertViewDelegate>

{

NSMutableArray * _dataSource;

}

- (void)viewDidLoad {

[super viewDidLoad];

_dataSource = [NSMutableArray array];

// Do any additional setup after loading the view, typically from a nib.

// self.view.backgroundColor = [UIColor whiteColor];

[self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"MYCELL"];

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"LastCell"];

self.collectionView.backgroundColor = [UIColor whiteColor];

self.collectionView.dataSource = self;

self.collectionView.delegate = self;

UIButton * btn = [[UIButton alloc]initWithFrame:

CGRectMake(100, CGRectGetHeight(self.view.bounds)-100, 50, 30)];

[btn setTitle:@"Add" forState:UIControlStateNormal];

btn.tag = 11;

btn.backgroundColor = [UIColor purpleColor];

[btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

UIButton * dbtn = [[UIButton alloc]initWithFrame:

CGRectMake(200, CGRectGetHeight(self.view.bounds)-100, 50, 30)];

[dbtn setTitle:@"Mod" forState:UIControlStateNormal];

dbtn.tag = 12;

dbtn.backgroundColor = [UIColor purpleColor];

[dbtn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:dbtn];

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

return  1;

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

return _dataSource.count + 1;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

//    for (UIView * view in self.collectionView.subviews) {

//        [view removeFromSuperview];

//    }

CollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MYCELL" forIndexPath:indexPath];

//    cell.tag = indexPath.row + 100;

if (indexPath.row < _dataSource.count) {

NSString * title =[_dataSource[indexPath.row] objectForKey:@"name"];

cell.titleLabel.text = title;

cell.numberLabel.text = [NSString stringWithFormat:@"%@g",title];

cell.deleButton.hidden = NO;

cell.deleButton.tag = 100 + indexPath.row;

[cell.deleButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

}

else if (indexPath.row == _dataSource.count) {

UICollectionViewCell *  laseCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LastCell" forIndexPath:indexPath];

UIView * lastView = [[UIView alloc]initWithFrame:cell.bounds];

laseCell.layer.borderWidth = 1;

laseCell.layer.borderColor = [UIColor greenColor].CGColor;

UILabel * lastLabel = [[UILabel alloc] initWithFrame: lastView.bounds];

lastLabel.textAlignment = NSTextAlignmentCenter;

lastLabel.text = @"点击添加新药品";

lastLabel.textColor = [UIColor grayColor];

[lastView addSubview: lastLabel];

[laseCell.contentView addSubview:lastView];

return laseCell;

}

return cell;

}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

return  CGSizeMake(200, 150);

}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {

return UIEdgeInsetsMake(5, 5, 5, 5);

}

//返回这个UICollectionView是否可以被选择

-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

return YES;

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

NSLog(@"%@",indexPath);

//将其他cell全部变色为不可选

for (int i = 0 ; i<_dataSource.count+1 ; i++) {

NSIndexPath * mIndexPath = [NSIndexPath indexPathForItem:i inSection:0];

CollectionViewCell * mcell = (CollectionViewCell *)[collectionView cellForItemAtIndexPath:mIndexPath];

mcell.layer.borderColor = [UIColor redColor].CGColor;

}

CollectionViewCell * cell = (CollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];

if (cell.layer.borderColor != [UIColor yellowColor].CGColor) {

cell.layer.borderWidth = 1;

cell.layer.borderColor = [UIColor yellowColor].CGColor;

} else {

cell.layer.borderColor = [UIColor redColor].CGColor;

}

//[self collectionView: self.collectionView didHighlightItemAtIndexPath:indexPath];

}

#pragma mark  -UICollertionViewDelegate-

//- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {

//

//}

//是否高亮,默认YES,否则不可点击

- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {

return  YES;

}

#pragma mark - UIAlertViewDelegate-

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

UITextField * tf =[ alertView textFieldAtIndex: 0];

NSMutableDictionary * myDic = _dataSource[0];

[myDic setObject:tf.text forKey:@"name"];

NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0  inSection:0];

[self.collectionView reloadItemsAtIndexPaths:@[indexPath]];

}

- (void) buttonPressed:(UIButton *)button{

if (button.tag == 11) {

NSMutableDictionary * dic = [NSMutableDictionary dictionary];

[dic setObject:[NSString stringWithFormat:@"%lu",(unsigned long)_dataSource.count] forKey:@"name"];

[_dataSource addObject:dic];

[self.collectionView reloadData];

} else if (button.tag == 12) {

UIAlertView * myAlert = [[UIAlertView alloc] initWithTitle:@"修改" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];

myAlert.alertViewStyle = UIAlertViewStylePlainTextInput;

[myAlert show];

}

//删除

else if (button.tag>= 100 && button.tag <=100 + _dataSource.count) {

[_dataSource removeObjectAtIndex:button.tag - 100 ];

[self.collectionView reloadData];

//        NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:button.tag];

//

//        [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];

}

}

UICollectionViewController的用法1的更多相关文章

  1. IOS中UICollectionView和UICollectionViewController的用法

    1.新建一个xib描述UICollectionViewCell(比如DealCell.xib),设置好resuse identifier(比如deal) 2.控制器继承UICollectionView ...

  2. UICollectionViewController用法

    在iOS 6 发布前,开发人员习惯使用UITableView来展示几乎所有类型的数据集合.ios 6 为 IOS 引入了全新的控制器,用来显示数据集合,集合视图控制器是与表视图控制器类似的全新UI框架 ...

  3. iOS开发之窥探UICollectionViewController(五) --一款炫酷的图片浏览组件

    本篇博客应该算的上CollectionView的高级应用了,从iOS开发之窥探UICollectionViewController(一)到今天的(五),可谓是由浅入深的窥探了一下UICollectio ...

  4. iOS开发之窥探UICollectionViewController(四) --一款功能强大的自定义瀑布流

    在上一篇博客中<iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流>,自定义瀑布流的列数,Cell的外边距,C ...

  5. iOS开发之窥探UICollectionViewController(一) -- Ready Your CollectionViewController

    之前用CollectionViewController只是皮毛,一些iOS从入门到精通的书上也是泛泛而谈.这几天好好的搞了搞苹果的开发文档上CollectionViewController的内容,亲身 ...

  6. UICollectionView在Swift3.0中的用法

    UICollectionView在Swift3.0中的用法 UICollectionView的初始化跟OC中是相似的,创建 GameView 集成自 UICollectionView .注意不同于UI ...

  7. iOS开发之UICollectionViewController

    1.概述 UICollectionView控件主要是用来做九宫格的,类似于android中的GridView控件.其用法与UITableView一样,首先要使控制器遵守数据源协议,再将控制器设置为UI ...

  8. EditText 基本用法

    title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...

  9. jquery插件的用法之cookie 插件

    一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...

随机推荐

  1. 【BZOJ3590】[Snoi2013]Quare 状压DP

    这道题...神题. 首先看到数据范围,一眼状压 dp .然后? 没了. 理性分析,这里说断掉任意一条边图依然连通,即整个图构成一个边双(而不是点双). 之前用 fire (机房里的随机算法总称)之所以 ...

  2. k8s技能树

  3. 设置Vmware中Kali_linux 共享文件夹

    kali_linux_2018.1 安装 Vmware-Tools 创建与 windows 的共享文件夹 vmware 设置共享目录 安装 Vmware-Tools root@kali:~# apt- ...

  4. 解决ftp客户端连接验证报错Server sent passive reply with unroutable address. Using server address instead

    最近在linux服务器安装vsftp服务.经过一轮设置,终于可以连接上了,用winSCP连接,刷新目录就提示这个错误. 解决办法: vim /etc/vsftpd.conf  ,编辑配置文件,最后加上 ...

  5. Go斐波拉契数列(Fibonacci)(多种写法)

    1 前言 斐波拉契数列有递归写法和尾递归和迭代写法. 2 代码 //recursion func fib(n int) int{ if n < 2{ return n }else{ return ...

  6. Android PermissionUtils:运行时权限工具类及申请权限的正确姿势

    Android PermissionUtils:运行时权限工具类及申请权限的正确姿势 ifadai 关注 2017.06.16 16:22* 字数 318 阅读 3637评论 1喜欢 6 Permis ...

  7. C# 制作向导

    1.FormBase界面:有“帮助,上一步,下一步,取消”按钮,这些按钮放置在一个Panel上. namespace DataBase {     public partial class FormB ...

  8. STM32L476应用开发之八:便携式气体分析仪项目总结

    在本次项目中,我们实现的实际上是2套设备:便携式氧气分析仪以及便携式甲烷分析仪.但这两台仪器实际使用的主控板我们是设计了一套,所以主控板是适合于这两个设备的. 1.硬件设计 便携式气体分析仪的功能比较 ...

  9. STM32应用实例十一:基于SPI和AD7192的数据采集

    在开发臭氧发生器的时,我们需要一个高分辨率的AD采集,于是选择了AD7192,选择这款ADC的原因比较简单.首先它是24位的符合我们的精度要求:其次它自带时钟,便于节省空间:第三他又4路单端或2路差分 ...

  10. java 中int与integer的区别

    int与integer的区别从大的方面来说就是基本数据类型与其包装类的区别: int 是基本类型,直接存数值,而integer是对象,用一个引用指向这个对象 1.Java 中的数据类型分为基本数据类型 ...