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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js数组的sort排序详解
- 下一篇: DWZ 验证 CLASS 规则