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

原创 2015年11月19日 18:04:34


#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






版权声明:本文为博主原创文章,未经博主允许不得转载。

用UICollectionView实图片瀑布流

  • 2015年07月10日 18:34
  • 190KB
  • 下载

UICollectionView 瀑布流 自定义cell 与表头表尾 自适应高度

//自定义cell //自定义表头和表尾 //根据原图片自定义cell的高度 #import "RootViewController.h"#import "GirlCollec...

UICollectionview实现瀑布流,

  • 2015年06月03日 13:46
  • 77KB
  • 下载

UICollectionView实现瀑布流布局

因为前面的文章已经对UICollectionVie和UICollectionViewLayout做了详细解说,在此就不再在次说明了,直接贴出UICollectionViewLayout类中的代码! ...
  • pbz106
  • pbz106
  • 2016年12月23日 11:42
  • 98

iOS UIcollectionview瀑布流

  • 2017年06月29日 11:12
  • 221KB
  • 下载

iOS UICollectionView实现瀑布流

  • 2015年05月10日 14:29
  • 17.58MB
  • 下载

UICollectionView实现瀑布流

ViewController.h文件 @interface ViewController : UIViewController @property (nonatomic, strong) UIColl...
  • ab20514
  • ab20514
  • 2015年11月09日 23:30
  • 227

iOS开发 UI UICollectionView 瀑布流

UICollectionView在2012年被提出,已经不是什么新技术了,在此只是做一下简单的实现。集合视图:UICollectionView UICollectionView和UITableVie...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:UICollectionView (仿系统相册,按日期分区 item瀑布流)
举报原因:
原因补充:

(最多只允许输入30个字)