CollectionView(一)
来源:互联网 发布:ios app启动时间优化 编辑:程序博客网 时间:2024/06/01 13:56
1.cell(继承于UICollectionViewCell)
cell.h文件
@interface Cell : UICollectionViewCell
@property (strong,nonatomic)IBOutlet UIImageView *image;
@property (strong,nonatomic)IBOutlet UILabel *label;
cell.m文件
#import"Cell.h"
#import"CustomCellBackground.h"
@implementation Cell
- (id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
CustomCellBackground *backgroundView = [[CustomCellBackground alloc] initWithFrame:CGRectZero];
self.selectedBackgroundView = backgroundView;
}
returnself;
}
2.CustomCellBackground(选中的背景)
.h文件
@interface CustomCellBackground :UIView
.m文件
#import "CustomCellBackground.h"
@implementation CustomCellBackground
- (void)drawRect:(CGRect)rect
{
// draw a rounded rect bezier path filled with blue
CGContextRef aRef =UIGraphicsGetCurrentContext();
CGContextSaveGState(aRef);
UIBezierPath *bezierPath = [UIBezierPathbezierPathWithRoundedRect:rectcornerRadius:5.0f];
[bezierPathsetLineWidth:5.0f];
[[UIColor blackColor] setStroke];
UIColor *fillColor = [UIColorcolorWithRed:0.529green:0.808 blue:0.922alpha:1];
[fillColorsetFill];
[bezierPathstroke];
[bezierPathfill];
CGContextRestoreGState(aRef);
}
@end
3.CollectionViewController(继承于UICollectionViewController)
.h文件
@interface CollectionViewController : UICollectionViewController
.m文件
#import "CollectionViewController.h"
#import "DetailViewController.h"
#import "Cell.h"
NSString *kDetailedViewControllerID =@"DetailView";
NSString *kCellID =@"cellID";
@implementationCollectionViewController
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
return 32;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
Cell *cell = [cvdequeueReusableCellWithReuseIdentifier:kCellIDforIndexPath:indexPath];
cell.label.text = [NSStringstringWithFormat:@"{%ld,%ld}", (long)indexPath.row, (long)indexPath.section];
NSString *imageToLoad = [NSStringstringWithFormat:@"%ld.JPG", (long)indexPath.row];
cell.image.image = [UIImageimageNamed:imageToLoad];
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segueidentifier]isEqualToString:@"showDetail"])
{
NSIndexPath *selectedIndexPath = [[self.collectionViewindexPathsForSelectedItems]objectAtIndex:0];
// 点击详情图片加载大图
NSString *imageNameToLoad = [NSStringstringWithFormat:@"%ld_full", (long)selectedIndexPath.row];
NSString *pathToImage = [[NSBundlemainBundle]pathForResource:imageNameToLoadofType:@"JPG"];
UIImage *image = [[UIImagealloc]initWithContentsOfFile:pathToImage];
DetailViewController *detailViewController = [seguedestinationViewController];
detailViewController.image = image;
}
}
@end
4.DetailViewController
@interface DetailViewController :UIViewController
@property (nonatomic,strong)UIImage *image;
.m文件
@interface DetailViewController ()
@property (nonatomic,weak)IBOutletUIImageView *imageView;
@end
@implementation DetailViewController
- (void)viewWillAppear:(BOOL)animated
{
[superviewWillAppear:animated];
self.imageView.image =self.image;
}
@end
- CollectionView 基础(一)创建系统collectionview
- CollectionView(一)
- CollectionView
- collectionView
- collectionView
- collectionView
- CollectionView
- CollectionView
- collectionView
- collectionView
- CollectionView
- collectionView
- collectionview
- 一分钟学会collectionView自定义layout(一)
- 纯代码collectionView(实训)
- 一分钟学会collectionView自定义layout(二、圆形布局)
- block与刷新(collectionView、tableview)
- iOS 7:漫谈集合视图(CollectionView)
- 求解最大公约数和最小公倍数问题
- 简单密码破解
- 蚂蚁过杆问题
- Sqoop1 的安装以及基本的使用方法
- 如何为类别增加变量?
- CollectionView(一)
- struts2的action的访问顺序
- android shape corners 四个属性无效解决
- CentOS 6.4 yum安装配置LNMP服务器(Nginx+PHP+MySQL)
- NEUQ1051: 谭浩强C语言(第三版)习题6.7
- 【BZOJ 3504】 [Cqoi2014]危桥
- logback 配置详解(一)
- chrome浏览器页面错位
- Facebook Fresco体验 (一)