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:rect cornerRadius:5.0f];
[bezierPathsetLineWidth:5.0f];
[[UIColor blackColor] setStroke];
UIColor *fillColor = [UIColorcolorWithRed:0.529 green:0.808 blue:0.922 alpha: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