iOS 自定义colletionView(纯代码)
来源:互联网 发布:淘宝定制的好不好 编辑:程序博客网 时间:2024/06/07 22:35
大家都说colletionView和UITabbleView 是兄弟,而且colletionView是在IOS 6之后出来的, colletionView和UITabbleView他俩确实是兄弟,但是使用的时你回遇到好多坑。
比如:
UICollectionView *colletionView = [[UICollectionView alloc]init];初始化一个colletionView,如果你这么搞,你就掉到坑里了。应为人家官方文档是这样给你的。
so!!!!你这样搞就崩掉。你只能这样。
UICollectionViewFlowLayout *grid =[[UICollectionViewFlowLayout alloc] init]; grid.itemSize = CGSizeMake(80, 80); //设置colletionView的大小grid.sectionInset = UIEdgeInsetsMake(10.0, 10, 10, 10);UICollectionView *colletionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:grid];colletionView.delegate =self; colletionView.dataSource = self;[colletionView registerClass:[photoCell class] forCellWithReuseIdentifier:@"simpleCell"];//这个一定要加不加上的化你的Cell init 是不会调用的哦 !!
OK 正就成功实例化了一个colletionView它与它的兄弟一样需要设置代理,设置数据源。在实例化的时候已经设置好了。
现在去实现他的代理并且自定义一个colletionViewCell
自定义Cell 要去自定义一个Cell类继承UICollectionViewCell
.h
#import <UIKit/UIKit.h>@interface Cell : UICollectionViewCell@property (nonatomic,strong)UIImageView *image;@end
.m
@implementation Cell-(id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // change to our custom selected background view self.image = [[UIImageView alloc]init]; [self.image setFrame:CGRectMake(self.contentView.frame.size.width/2+20+5, -5, 20, 20)]; [self.image setBackgroundColor:[UIColor redColor]]; self.image.layer.cornerRadius=self.image.frame.size.width/2; // 将图层的边框设置为圆角 self.image.layer.masksToBounds=YES; // 隐藏边界 [self addSubview:self.image]; } return self;}@end
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 1;}-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return 10;}-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellIdentifier = @"simpleCell"; Cell *cell = (Cell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; if (cell ==nil) { cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; } return cell;}- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ //--单机事件}
1 0
- iOS 自定义colletionView(纯代码)
- iOS 纯代码写ColletionView
- iOS开发——纯代码界面(自定义UITableViewCell)
- 复习ios --纯代码自定义 UITableviewCell
- ios开发 使用纯代码自定义UItableviewcell
- iOS中ColletionView的简单使用
- 猫猫学IOS(十七)UI之纯代码自定义Cell实现新浪微博UI
- AJ学IOS(17)UI之纯代码自定义Cell实现新浪微博UI
- iOS个人整理20-UITableViewCell自定义,cell高度的自适应(纯代码)
- iOS纯代码自定义UITableViewCell及性能优化
- 纯代码自定义UITableViewCell
- 自定义tabbar(纯代码)
- 纯代码写iOS
- iOS 纯代码跳转
- iOS动画纯代码
- (素材源码)猫猫学IOS(十七)UI之纯代码自定义Cell实现新浪微博UI
- 纯代码自定义radiobutton样式
- 使用纯代码自定义cell
- TCP打洞技术
- iOS——播放声音和振动
- erlang创建进程
- 长按桌面进入小部件,选择“设置快捷方式”里任意一个添加到桌面,在设置中将语音设置为英语,添加的快捷方式依然是中文
- 线程同步
- iOS 自定义colletionView(纯代码)
- 简单说一下java中计时器,实际需要,没有详解
- js/jQuery实现类似百度搜索功能
- Linux_3.2_动态函数库设计
- c语言:typedef + struct + 指针 http://blog.sina.com.cn/s/blog_5f70c7060101201e.html
- 使用dwr3.0实现服务端向浏览器做消息推送,做滚动评论或弹幕效果,而且根据视频id做推送消息拦截功能
- !!Js中Prototype、__proto__、Constructor、Object、Function关系介绍
- ggplot2 学习
- 2.LDT