日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UICollectionViewController的用法1

發(fā)布時(shí)間:2025/7/14 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UICollectionViewController的用法1 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

UICollectionView 和 UICollectionViewController 類是iOS6 新引進(jìn)的API,用于展示集合視圖,布局更加靈活,可實(shí)現(xiàn)多列布局,用法類似于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 = @"點(diǎn)擊添加新藥品";

? ? ? ? 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);

}

//返回這個(gè)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 {

//

//}

//是否高亮,默認(rèn)YES,否則不可點(diǎn)擊

- (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]];

? ? }

?

}

轉(zhuǎn)載于:https://www.cnblogs.com/qzp2014/p/4256101.html

總結(jié)

以上是生活随笔為你收集整理的UICollectionViewController的用法1的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲AV成人无码电影在线观看 | 杨幂一区二区国产精品 | 欧洲在线一区 | 9色在线视频 | 日韩夜夜 | 老熟妇毛片 | 久一视频在线观看 | 性xxxx另类xxⅹ | 日本在线不卡一区 | jizz亚洲女人| 欧美色图亚洲自拍 | 97黄色片| 超碰五月天 | 亚洲aaaaa特级| 亚洲天天操 | 国产伦精品一区二区三区视频黑人 | 最新视频 - 88av | 午夜亚洲一区 | 黄色a级片在线观看 | 中文av资源 | 手机在线成人av | 亚洲av片一区二区三区 | 日本乱论视频 | 亚洲影视精品 | 久久久全国免费视频 | 中国美女洗澡免费看网站 | 日韩精品在线网站 | 一区二区三区视频观看 | 精品人妻一区二区三区蜜桃视频 | 污视频在线播放 | 国产拍拍拍拍拍拍拍拍拍拍拍拍拍 | 九九激情网 | 99在线免费观看视频 | 动漫美女被吸乳奶动漫视频 | 奇米网狠狠干 | 午夜三级网站 | 欧洲精品在线播放 | 在线一区二区视频 | 日本乱码一区 | 91精品国产综合久久久蜜臀图片 | 亚洲欧美日韩久久 | 亚洲精品成人久久 | 波多野结衣精品 | 亚州av成人| 国产一区二区99 | 91黄色小网站 | 国产又大又粗又长 | 亚洲狠狠 | 日韩免费一二三区 | 美女131爽爽爽 | 欧美精品免费看 | 日韩免费毛片 | 久久久性色精品国产免费观看 | www.国产com | 久草这里只有精品 | 国产在线国偷精品免费看 | 69精品视频 | 久久综合色视频 | 无码精品国产一区二区三区免费 | aaa天堂| 欧美激情免费 | 亚洲欧美bt | 黄色三及 | 在线观看av的网站 | 又黄又高潮的视频 | 超碰在线进入 | 中文字幕影院 | 亚洲欧美中文日韩在线观看 | 国产精品麻豆一区二区 | 日韩高清在线观看 | 成人激情视频在线播放 | 日日射天天干 | 欧美 日韩 国产 一区二区三区 | 国产精品一区视频 | 亚洲丁香婷婷 | 蜜桃视频在线网站 | 日韩精品一区二区三区中文字幕 | av最新版天堂资源在线 | 少妇一级淫片 | 又大又长粗又爽又黄少妇视频 | 天天射日日射 | 日韩欧美不卡在线 | 伊人网大 | 污片免费在线观看 | 日本天堂网在线 | 天天狠天天插 | 欧美精品一区二区三区在线 | 午夜看片网站 | 亚洲综合在线播放 | 久久在线免费观看视频 | xxx老太太 | 91啪在线观看 | 一区二区伦理片 | 久久久久久久久久影院 | 亚洲国产精品久久人人爱 | 日韩av一区二区三区在线观看 | 91在现看 | 激情四射网 | 男人天堂影院 |