您的位置:首页 > 产品设计 > UI/UE

UICollectionView (仿系统相册,按日期分区 item瀑布流)

2015-11-19 18:04 513 查看
#import "PhotoManageCell.h"
#import "footView.h"

@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate,PhotoManageCellEditDelegate,PhotoManageCellSelectedDelegate>

@property(nonatomic,retain) footView *footer_View;

@end

static NSString *CellIdentifier = @"Cell";
@interface ViewController (){
NSMutableArray *listAry;
UICollectionView *collection_View;
UIButton *deleteBtn;
NSMutableArray *imgIdAry;
}

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
[self modelListAry];
[self buildCollectionView];
// Do any additional setup after loading the view, typically from a nib.
}

-(void)modelListAry
{
NSArray *ary1 = [NSArray arrayWithObjects:@"5",@"4",@"3",@"2",@"1", nil];
NSArray *ary2 = [NSArray arrayWithObjects:@"4",@"3",@"2",@"1", nil];
NSArray *ary3 = [NSArray arrayWithObjects:@"3",@"2",@"1", nil];
NSArray *ary4 = [NSArray arrayWithObjects:@"2",@"1", nil];
NSArray *ary5 = [NSArray arrayWithObjects:@"1", nil];

NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:ary1,@"value",@"2015820",@"index",nil];
NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:ary5,@"value",@"2015810",@"index",nil];
NSDictionary *dic3 = [NSDictionary dictionaryWithObjectsAndKeys:ary4,@"value",@"2015828",@"index",nil];
NSDictionary *dic4 = [NSDictionary dictionaryWithObjectsAndKeys:ary3,@"value",@"2015824",@"index",nil];
NSDictionary *dic5 = [NSDictionary dictionaryWithObjectsAndKeys:ary2,@"value",@"2015805",@"index",nil];
listAry = [NSMutableArray arrayWithObjects:dic1,dic2,dic3,dic4,dic5, nil];

//数组排序
NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"index" ascending:YES]];
[listAry sortUsingDescriptors:sortDescriptors];
}

-(void)buildCollectionView
{

if (!collection_View)
{
__weak ViewController *photoManageVC = self;
collection_View = [self createCollectionView];
collection_View.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:collection_View];
collection_View.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
}
else
{
[collection_View reloadData];
}
}

-(UICollectionView *)createCollectionView
{

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
float cellWidth=(self.view.bounds.size.width-30)/3;
float cellHeight=100;
//计算每页多少个,设置为2页的个数
[layout setItemSize:CGSizeMake(cellWidth, cellHeight)];
//设置其布局方向 垂直显示
[layout setScrollDirection:UICollectionViewScrollDirectionVertical];
layout.sectionInset = UIEdgeInsetsMake(10,10,10,10);
layout.minimumLineSpacing = 5;
layout.minimumInteritemSpacing = 5;

UICollectionView *collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0,64,CGRectGetWidth(self.view.frame),self.view.frame.size.height-64) collectionViewLayout:layout];
collectionView.dataSource=self;
collectionView.delegate=self;
collectionView.backgroundColor = [UIColor clearColor];
collectionView.alwaysBounceVertical = YES;
[collectionView registerClass:[PhotoManageCell class] forCellWithReuseIdentifier:CellIdentifier];

layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width-20, 40.0f);
[collectionView registerClass:[footView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"foot"];

return collectionView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [[[listAry objectAtIndex:section] objectForKey:@"value"] count];
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return [listAry count];
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

PhotoManageCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.editDelegate = self;
cell.selectedDelegate = self;
if (cell==nil) {
cell = [[PhotoManageCell alloc]init];
}
NSArray *ary = [[listAry objectAtIndex:indexPath.section] objectForKey:@"value"];
[cell setInfoData:ary index:indexPath.item];

return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

NSDictionary *indexDic = [listAry objectAtIndex:indexPath.item];

//选取图片
//    if ([self.selectedImgDelegate respondsToSelector:@selector(didSelectedImgBack:imageURl:imageId:)])
//    {
//        NSURL *smallImgUrl = [NSURL URLWithString:[indexDic objectForKey:@"imgSrc"]];
//        NSString *imageId = [indexDic objectForKey:@"imgId"];
//        [self.selectedImgDelegate didSelectedImgBack:self imageURl:smallImgUrl imageId:imageId];
//        return;
//    }
//
//    //进入预览大图
//    NSURL *bigImgUrl = [NSURL URLWithString:[indexDic objectForKey:@"imgSrc"]];
//    BigImageViewController *bigImgVC = [[BigImageViewController alloc]init];
//    bigImgVC.imageUrl = bigImgUrl;
//
//    [self presentViewController:bigImgVC animated:YES completion:^{}];

}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{

if (kind == UICollectionElementKindSectionHeader) {
self.footer_View = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"foot" forIndexPath:indexPath];
[self.footer_View setInfoData:[listAry objectAtIndex:indexPath.section]  index:indexPath.item];

return self.footer_View;

}
return 0;
}


@protocol PhotoManageCellEditDelegate <NSObject>
@optional
-(void)editCellBegin:(NSUInteger)object;

@end

@protocol PhotoManageCellSelectedDelegate <NSObject>
@optional
-(void)selectedCell:(UIButton*)button index:(NSUInteger)index;

@end

@interface PhotoManageCell : UICollectionViewCell<UIGestureRecognizerDelegate>{
UILongPressGestureRecognizer *longPress;
}

@property(nonatomic,retain)UIImageView *productImg;
@property(nonatomic,retain)UIButton *selBtn;
@property(nonatomic,assign) id<PhotoManageCellEditDelegate> editDelegate;
@property(nonatomic,assign) id<PhotoManageCellSelectedDelegate> selectedDelegate;

-(void)setInfoData:(id)data index:(NSUInteger)index;

@end

#define k_LongTapTime 0.6f
#define btn_tag 40000
#import "PhotoManageCell.h"
#import "UIImageView+WebCache.h"
@implementation PhotoManageCell

-(instancetype)initWithFrame:(CGRect)frame
{
if (self==[super initWithFrame:frame]){

self.backgroundColor = [UIColor clearColor];
longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(beginEdit:)];
longPress.minimumPressDuration = k_LongTapTime;
longPress.delegate = self;
[self addGestureRecognizer:longPress];

self.productImg = [[UIImageView alloc]init];
self.productImg.backgroundColor = [UIColor redColor];
[self addSubview:self.productImg];

self.selBtn = [[UIButton alloc]init];
[self.selBtn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
[self.selBtn setBackgroundImage:[UIImage imageNamed:@"selectBtn"] forState:UIControlStateSelected];
[self.selBtn addTarget:self action:@selector(selectedItem:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.selBtn];

}
return self;
}

-(void)selectedItem:(UIButton*)button
{
button.selected = !button.selected;
if (self.selectedDelegate)
{
[self.selectedDelegate selectedCell:button index:button.tag-btn_tag];
}
}

-(void)beginEdit:(UILongPressGestureRecognizer*)longPressGes
{
if (longPressGes.state == UIGestureRecognizerStateBegan)
{
NSLog(@"开始编辑可选");
if (self.editDelegate)
{
[self.editDelegate editCellBegin:longPress.view.tag];
}
}
if (longPressGes.state == UIGestureRecognizerStateEnded)
{
NSLog(@"结束");
}
}

/*
"imgId":"", //图片ID
"imgSmall":"", //小图,
"imgBig":"", //大图
"imgSrc":"" //原图
*/
-(void)setInfoData:(id)data index:(NSUInteger)index
{
// [self.productImg sd_setImageWithURL:[data objectForKey:@"imgSmall"] placeholderImage:[UIImage imageNamed:@"productimg"] options:SDWebImageRetryFailed|SDWebImageLowPriority];

// self.selBtn.selected = [[data objectForKey:@"isSelected"] boolValue];
[self.selBtn setTitle:[data objectAtIndex:index] forState:UIControlStateNormal];
[self.selBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// self.selBtn.userInteractionEnabled = [[data objectForKey:@"isEnable"] boolValue];
self.selBtn.tag = btn_tag+index;
self.tag = index;
}

-(void)layoutSubviews
{
[super layoutSubviews];

float cell_width = CGRectGetWidth(self.frame);
float cell_height = CGRectGetHeight(self.frame);

self.productImg.frame = CGRectMake(0, 0, cell_width, cell_height);
self.selBtn.frame = CGRectMake(0, 0, cell_width, cell_height);

}
@interface footView : UICollectionReusableView{

}

@property(nonatomic,assign) BOOL hasMoreData;
@property(nonatomic,retain) UILabel *lbl;

-(void)setInfoData:(id)data index:(NSUInteger)index;

@end

#import "footView.h"
@implementation footView

-(instancetype)initWithFrame:(CGRect)frame
{
if (self=[super initWithFrame:frame])
{
self.backgroundColor = [UIColor blueColor];
self.lbl = [[UILabel alloc]init];
self.lbl.textColor = [UIColor blackColor];

[self addSubview:self.lbl];
}
return self;
}

-(void)setInfoData:(id)data index:(NSUInteger)index;
{
self.lbl.text = [data objectForKey:@"index"];
self.lbl.tag = index;
}

-(void)setHasMoreData:(BOOL)hasMoreData
{

}

-(void)layoutSubviews
{
[super layoutSubviews];
self.lbl.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
}

@end


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息