CollectionView(一)

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


.h文件

@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













  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值