OC Waterfall can be expanded HEADVIEW_OC

Source: Internet
Author: User
Tags uikit

1.swvideocollectionview.h encapsulates CollectionView in the Swvideocollectionview class.

#import <UIKit/UIKit.h>


@protocol swvideocollectionviewdelegate <NSObject>

/**

* Click on the cell method

*/

-(void) Videocollectionview: (UIView *) View index: (nsinteger) index;

/**

* Click the Avatar method

*/

-(void) Videocollectionphotoview: (UIView *) View index: (nsinteger) index;

@optional

-(Uicollectionreusableview *) CollectionView: (Uicollectionview *) CollectionView Viewforsupplementaryelementofkind :(NSString *) kind Atindexpath: (Nsindexpath *) Indexpath;

-(void) Scrollviewdidscroll: (UIView *) View ScrollView: (Uiscrollview *) ScrollView Contentoffset: (cgpoint) Contentoffset;

@end

@interface Swvideocollectionview:uiview

-(Instancetype) initWithFrame: (CGRect) frame headerreferencesize: (cgsize) headerreferencesize headClass: (NSString *) Headclass;

@property (nonatomic,weak) Uicollectionview *collectionview;

@property (Nonatomic,strong) Nsarray *datalists;

@property (assign) id<swvideocollectionviewdelegate>delegate;

@end

1.swvideocollectionview.m

#import "SWVideoCollectionView.h"

#import "SWImgCollectionFlowLayout.h"

#import "SWMainDetailCollectionViewCell.h"

#import "SWVideoListModel.h"

#define CELLCOUNT 2

@interface Swvideocollectionview () <uicollectionviewdatasource,uicollectionviewdelegate, Uicollectionviewdelegateflowlayout,swmaindetailcollectionviewcelldelegate>

@property (nonatomic,copy) NSString *headclass;

@end

Static NSString *maindetailid =@ "Maindetailid";

Static NSString *headscrollid =@ "Headscrollid";

@implementation Swvideocollectionview

-(Instancetype) initWithFrame: (CGRect) frame headerreferencesize: (cgsize) headerreferencesize headClass: (NSString *) Headclass {

if (self = [superinitwithframe:frame]) {

Swimgcollectionflowlayout *layout = [[Swimgcollectionflowlayoutalloc] init];

if (headerreferencesize.height!=0 && headerreferencesize.width!=0) {

Layout.headerreferencesize = headerreferencesize;

}

Layout.columncount =cellcount;

Layout.minimumlinespacing =kkpadding * 0.5;

Layout.minimuminteritemspacing =kkpadding *0.5;

Layout.sectioninset =uiedgeinsetsmake (kkpadding *0.5, kkpadding *0.5, kkpadding *0.5, kkPadding *0.5);

Uicollectionview *collectionview = [[Uicollectionviewalloc] initWithFrame:self.boundscollectionViewLayout:layOut];

Collectionview.tag =self.tag;

Self.collectionview = CollectionView;

Collectionview.showshorizontalscrollindicator =no;

Collectionview.showsverticalscrollindicator =no;

[CollectionView Registerclass:[swmaindetailcollectionviewcellclass] forcellwithreuseidentifier:maindetailid];

if (Headclass!=nil) {

Self.headclass = Headclass;

[CollectionView registerclass:nsclassfromstring (Headclass) Forsupplementaryviewofkind: Uicollectionelementkindsectionheaderwithreuseidentifier:headscrollid];

}

Collectionview.backgroundcolor =KKLIGHTGRAYBG;

Collectionview.datasource =self;

Collectionview.delegate =self;

[Selfaddsubview:collectionview];

}

returnself;

}

#pragma mark-collectionview Agent

-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) Section {

if (!self.datalists | | Self.dataLists.count ==0) {

Swvideolistmodel *model = [[Swvideolistmodelalloc] init];

model.goods_id =@ "-1";

Self.datalists = [Nsarrayarraywithobjects:model, nil];

}

Returnself.dataLists.count;

}

-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath {

Swmaindetailcollectionviewcell *cell = [Collectionviewdequeuereusablecellwithreuseidentifier: Maindetailidforindexpath:indexpath];

Cell.tag = Indexpath.row;

Cell.delegate =self;

Cell.videomodel =self.datalists[indexpath.row];

return cell;

}

#pragma mark-click cell

-(void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) IndexPath {

Swvideolistmodel *listmodel =self.datalists[indexpath.row];

if ([listmodel.goods_idisequaltostring:@ "-1"]) {

Return

}

[Self.delegatevideoCollectionView:selfindex:indexPath.row];

}

#pragma mark-click Avatar

-(void) Maindetailcollectionviewcell: (Uicollectionviewcell *) Cell {

Swvideolistmodel *listmodel =self.datalists[cell.tag];

if ([listmodel.goods_idisequaltostring:@ "-1"]) {

Return

}

[Self.delegatevideoCollectionPhotoView:selfindex:cell.tag];

}

The size of the #pragma mark-cell is used for waterfall flow computed values

-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout *) collectionviewlayout Sizeforitematindexpath: (Nsindexpath *) Indexpath {

Swvideolistmodel *info =self.datalists[indexpath.row];

if ([info.goods_idisequaltostring:@ "-1"]) {

Returncgsizemake (0,0);

}

CGFloat itemwidth = (Self.frame.size.width-(cellcount + 1) *kkpadding * 0.5)/cellcount;

CGFloat ITEMH = itemwidth;

if (info.default_image_height >0 && info.default_image_width >0) {

ITEMH = [Info.default_image_heightfloatvalue] * itemwidth/[info.default_image_widthfloatvalue];

}

Other text height settings

CGFloat Marin =kkpadding * 1.5;

CGFloat txtheight = marin;//distance from bottom margin

TxtHeight + 35 *0.5;//head height

CGFloat titleheight = [info._descriptionboundingheightwithwidth:itemwidth-kkpadding dict:kkFont13];

TxtHeight + = Titleheight;

ITEMH + = TxtHeight;

Returncgsizemake (Itemwidth, ITEMH);

}

#pragma mark-Expand header File optional

-(Uicollectionreusableview *) CollectionView: (Uicollectionview *) CollectionView Viewforsupplementaryelementofkind :(NSString *) kind Atindexpath: (Nsindexpath *) Indexpath {

return [Self.delegatecollectionView:collectionViewviewForSupplementaryElementOfKind:kindatIndexPath:indexPath];

}

#pragma mark-Listening for scrolling offset optional

-(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView {

if ([Self.delegaterespondstoselector: @selector (scrollViewDidScroll:scrollView:contentOffset:)]) {

[Self.delegatescrollViewDidScroll:selfscrollView:scrollView ContentOffset:scrollView.contentOffset];

}

}

#pragma mark-setting up a data source

-(void) Setdatalists: (Nsarray *) datalists {

_datalists = datalists;

[Self.collectionviewreloaddata];

}

@end

3. Custom Swheadscrollviewcell, Swmaindetailcollectionviewcell. Implement Maindetailcollectionviewcell Agent. Rewrite layoutsubviews

4. Custom Uicollectionviewflowlayout UICOLLECTIONVIEWFLOWLAYOUT.M

#import "SWImgCollectionFlowLayout.h"


@interface Swimgcollectionflowlayout ()

@property (Nonatomic,strong) nsmutabledictionary *attributes;

@property (Nonatomic,strong) Nsmutablearray *colarr;

@property (nonatomic,assign) uiedgeinsets edgeinsets;

@property (Nonatomic,weak) id<uicollectionviewdelegateflowlayout> delegate;

@property (Nonatomic,strong) Nsmutablearray *layoutattributesarray;

@end


@implementation Swimgcollectionflowlayout


-(void) Preparelayout {

Class

Self.attributes = [[Nsmutabledictionaryalloc] init];

Self.layoutattributesarray = [Nsmutablearrayarray];

Self.colarr = [NSMutableArrayarrayWithCapacity:self.columnCount];

Self.delegate = (id<uicollectionviewdelegateflowlayout>) self.collectionView.delegate;

Get the total number

Nsuinteger itemCount = [self.collectionviewnumberofitemsinsection:0];

if (!itemcount) {

Return

}

_colarr is used to store adjacent three heights.

CGFloat top =.0f;

For (Nsuinteger idx =0; idx < self.columncount; idx++) {

[_colarraddobject:[nsnumbernumberwithfloat:top]];

}

Append header file layout.

Nsarray *attributesarray1 = [Superlayoutattributesforelementsinrect:cgrectmake (0,0, Self.collectionview.frame.size.width,self.collectionview.frame.size.height)];

For (uicollectionviewlayoutattributes *headattributesin attributesArray1) {

if (Headattributes.representedelementkind ==uicollectionelementkindsectionheader) {

[Self.layoutAttributesArrayaddObject:headAttributes];

}

}

Iterate through all the item and rearrange the layout

For (Nsuinteger idx =0; idx < itemCount; idx++) {

[Selflayoutitematindexpath:[nsindexpathindexpathforitem:idx insection:0]];

}

}


//

-(void) Layoutitematindexpath: (Nsindexpath *) Indexpath {

Get the CollectionView edgeinsets

Uiedgeinsets edgeinsets =self.sectioninset;

if ([Self.delegaterespondstoselector: @selector (collectionView:layout:insetForSectionAtIndex:)]) {

Edgeinsets = [Self.delegatecollectionView:self.collectionViewlayout:selfinsetForSectionAtIndex:indexPath.row];

}

Self.edgeinsets = edgeinsets;

Get the CollectionView itemsize

Cgsize itemsize =self.itemsize;

if ([Self.delegaterespondstoselector: @selector (CollectionView:layout:sizeForItemAtIndexPath:)]) {

ItemSize = [Self.delegatecollectionView:self.collectionViewlayout:selfsizeForItemAtIndexPath:indexPath];

}

Traverse adjacent three heights to get the minimum height

Nsuinteger col = 0;

CGFloat shortheight = [[_colarrfirstobject] floatvalue];

For (Nsuinteger idx =0; idx < _colarr.count; idx++) {

CGFloat height = [_colarr[id

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.