UICollectionViewController的用法1
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]];
}
}
转载于:https://www.cnblogs.com/qzp2014/p/4256101.html
UICollectionViewController的用法1相关推荐
- iOS开发之窥探UICollectionViewController(四) --一款功能强大的自定义瀑布流
在上一篇博客中<iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流>,自定义瀑布流的列数,Cell的外边距,C ...
- c语言中external,static关键字用法
static用法: 在C中,static主要定义全局静态变量.定义局部静态变量.定义静态函数. 1.定义全局静态变量:在全局变量前面加上关键字static,该全局变量变成了全局静态变量.全局静态变量有 ...
- Pandas_transform的用法
先来看一个实例问题. 如下销售数据中展现了三笔订单,每笔订单买了多种商品,求每种商品销售额占该笔订单总金额的比例.例如第一条数据的最终结果为:235.83 / (235.83+232.32+107.9 ...
- Python中yield和yield from的用法
yield 后面接的是 future 对象 调用方 委托生成器 yield from 直接给出循环后的结果 yield from 委托者和子生成器直接通信 yield from 直接处理stopIte ...
- pytorch学习 中 torch.squeeze() 和torch.unsqueeze()的用法
squeeze的用法主要就是对数据的维度进行压缩或者解压. 先看torch.squeeze() 这个函数主要对数据的维度进行压缩,去掉维数为1的的维度,比如是一行或者一列这种,一个一行三列(1,3)的 ...
- python yield 和 yield from用法总结
#例1. 简单输出斐波那契數列前 N 个数 #缺点:该函数可复用性较差,因为 fab 函数返回 None,其他函数无法获得该函数生成的数列 #要提高 fab 函数的可复用性,最好不要直接打印出数列,而 ...
- tf.nn.embedding_lookup()的用法
函数: tf.nn.embedding_lookup( params, ids, partition_strategy='mod', name=None, validate_indices=True, ...
- OpenMP用法大全
OpenMP基本概念 OpenMP是一种用于共享内存并行系统的多线程程序设计方案,支持的编程语言包括C.C++和Fortran.OpenMP提供了对并行算法的高层抽象描述,特别适合在多核CPU机器上的 ...
- Dorado用法与示例
Dorado用法与示例 dorado用后总结 一.dorado概念 dorado的产品全名是"dorado展现中间件".从产品形态上dorado由两部分组成,第一部分是一个具有AJ ...
最新文章
- c枚举类型enum例题_SystemVerilog数据类型
- 关于数据结构的赋值,最好不要直接赋值,而是采用构造函数、或其它函数来赋值,这样可以方便控制---我自己!...
- POJ - 2559 Largest Rectangle in a Histogram(单调栈)
- python小屋_1000道Python题库系列分享九(31道)
- IJCAI阿里论文 | JUMP: 一种点击和停留时长的协同预估器...
- java线程volatile_多线程与高并发(四)volatile关键字
- 涨跌停计算器_股票涨跌停计算器
- 电力系统微型计算机继电保护试题及答案,2011年4月全国自学考试电力系统微型计算机继电保护试题答案...
- selenium+numpy+opencv突破滑块验证码实现QQ空间登入
- 手写原笔迹输入_原笔迹手写软件
- Failed to instantiate [com.XXX.entity.People]
- RuntimeWarning: divide by zero encountered in log错误解决
- android 尺寸
- 计算机网络微课堂笔记
- 博客:固定位置增加二维码
- 敏捷项目管理的五个要素
- MySQL:日期和时间函数
- htc 8x android,HTC 8X三款手机曝光:相机是重点 不支持SD卡
- 中国计算机软件工程大学专业,全国开设软件工程专业院校有哪些 都有什么大学名单...
- “金字塔原理”-写作