Reusable Waterfall Stream View

Source: Internet
Author: User

STEP1 = = = Package Custom Uiscrollview Controller

1 Protocol Proxy method

1>> Data Source Proxy Pollscrolldatasource @protocol pollscrolldatasource <nsobject>@required//Returns the total number of columns- (Nsinteger) Numberofcolums: (Uiscrollview*) Jrscroll;//Returns the total number of cells- (Nsinteger) Numberofcells: (Uiscrollview*) Jrscroll;//Returns the actual width height of each cell- (cgsize) Sizeofcellinscroll: (Uiscrollview*) Jrscroll Cellatindex: (Nsinteger) index;//Return to Cell view- (UIView*) Jrscroll: (Uiscrollview*) Jrscroll Cellatindex: (Nsinteger) index;@end2>> Property Agent Pollscrolldelegate @protocol pollscrolldelegate <nsobject>@optional//Return line spacing- (CGFloat) Rowofscroll: (Uiscrollview*) Jrscroll Spaceforcellwithflag: (pollscrollspace) Jrspace;@end3>> Define Agents@property(nonatomic,Weak)ID<PollScrollDataSource> Polldatasource;@property(nonatomic,Weak)ID<PollScrollDelegate> polldelegate;

2 Waterfall Flow view, calculating the location size of each cell

1>> each cell size location obtained by the proxy method//enum type set margin#define KSpacetypedef enum{Topscrollspace,//Top marginBottomscrollspace,//Bottom marginLeftscrollspace,//Left marginRightscrollspace,//Right marginRowscrollspace,//Row marginsColumnscrollspace//Column margins}pollscrollspace;//Get the total number of cells    Nsintegertotalcell=[ Self. PolldatasourceNumberofcells: Self];//Gets the total number of columns--if column = 0, returns the default value of 3    Nsintegertotalcolum=[ Self. PolldatasourceNumberofcolums: Self];if(!totalcolum) {totalcolum=3; }//Get up, down, left, right, row, column spacing---if there is no response to implement the Proxy method, set the default value    CGFloatTopspace=kspace,bottomspace=kspace,leftspace=kspace,rightspace=kspace,rowspace=kspace,columnspace=kspace;if([ Self. PolldelegateRespondstoselector:@selector(Rowofscroll:spaceforcellwithflag:)]) {topspace=[ Self. PolldelegateRowofscroll: SelfSpaceforcellwithflag:topscrollspace]; bottomspace=[ Self. PolldelegateRowofscroll: SelfSpaceforcellwithflag:bottomscrollspace]; leftspace=[ Self. PolldelegateRowofscroll: SelfSpaceforcellwithflag:leftscrollspace]; rightspace=[ Self. PolldelegateRowofscroll: SelfSpaceforcellwithflag:rightscrollspace]; rowspace=[ Self. PolldelegateRowofscroll: SelfSpaceforcellwithflag:rowscrollspace]; columnspace=[ Self. PolldelegateRowofscroll: SelfSpaceforcellwithflag:columnscrollspace]; }CGFloatCoorarray[totalcolum]; for(intI=0; i<totalcolum; i++) {//re-initialize the arraycoorarray[i]=0; } for(intI=0;i<totalcell; i++) {//Calculate all frame        //Find the lowest location        CGFloatheight=coorarray[0];Nsintegerindex=0; for(intj=0; j<totalcolum; J + +) {if(Coorarray[j] < height)                {HEIGHT=COORARRAY[J];            Index=j; }        }//Calculate frame        //Get the actual width and height of the image, which is the actual width and height obtained from the plist        cgsizeframe=[ Self. PolldatasourceSizeofcellinscroll: SelfCELLATINDEX:I];//Set the width and height of each cell         Self. Cellwidth=(( Self. Frame. Size. Width-leftspace-rightspace)-columnspace* (totalcolum-1))/totalcolum; Self. Cellheight= Self. Cellwidth*frame. Height/frame. Width;//w h determines the width by spacing, and then scales to get the height        //spacing between adjacent cells        CGFloatVspace= Self. Cellheight+rowspace;distance between//two vertical cells        CGFloatHspace= Self. Cellwidth+columnspace;//Two horizontal cell distance  //where each cell is located        CGRectFrames=cgrectmake (Index*hspace+leftspace,height+rowspace, Self. Cellwidth, Self. Cellheight); Nsvalue * Value=[nsvalue Valuewithcgrect:frames];// update array DataCoorarray[index]=cgrectgetmaxy (frames); [ Self. FramearrayAddobject:value]; }//Get the maximum height    CGFloatmaxheight=coorarray[0]; for(intj=0; j<totalcolum; J + +) {if(Coorarray[j]>maxheight)        {MAXHEIGHT=COORARRAY[J]; }    } Self. Contentsize=cgsizemake (0, maxheight+Ten);

3 Waterfall flow views that can be reused

1>> Render Waterfall Stream View Method-(void) layoutsubviews{[SuperLayoutsubviews]; for(intI=0; i< Self. Framearray. Count; i++) {Nsvalue * value= Self. Framearray[i];CGRectFrame=[value Cgrectvalue];BOOLisinscreen=[ Self_isinscreen:frame];if(Isinscreen) {//On the screen there are two situations---1> have been added before (do not process)--2> just entered the screen (join the dictionary)            if(! Self. Inscreendic[@ (i)]) {//Not appearing on the screen before the join dictionary                UIView* view=[ Self. PolldatasourceJrscroll: SelfCELLATINDEX:I]; View. Frame=frame; [ SelfAddsubview:view];//load to the field of view screen[ Self. InscreendicSetobject:view forkey:@ (i)];//Add to Dictionary}         }Else{//If you leave the screen, remove it from the parent view of the screen and remove it from the dictionary            UIView* cell= Self. Inscreendic[@ (i)];if(Jrcell) {//Join to the cache poolNsmutableset * set= Self. Pooldic[Jrcell. Identy];                [Set Addobject:jrcell]; [Jrcell Removefromsuperview];//Remove from screen[ Self. Inscreendicremoveobjectforkey:@ (i)];//Remove from dictionary}        }    }}2>> Judging is not on the screen-(BOOL) _isinscreen: (CGRect) frame{CGFloatcontenty= Self. Contentoffset. Y;BOOLCondition=cgrectgetmaxy (frame) >contenty && frame. Origin. Y<contenty+ Self. Frame. Size. Height;returncondition;}

4 Getting a cell view that can be reused

- (UIView *)dequeueReuseCellWithIdenty:(NSString *)identy{    set=self.poolDic[identy];    if (set==nil) {        set=[NSMutableSet set];        [self.poolDic setObject:set forKey:identy];    }    UIView * view=[set anyObject];    if (view) {        [set removeObject:view];    }    return view;}

Step2 = = = Waterfall Stream implementation

1>> Load data (plist file)-(void) _loaddata{//Get data into array from plist    NSString* path=[[NSBundlemainbundle]pathforresource:@"File.plist"OfType:Nil]; Self. DataArray=[NsarrayArraywithcontentsoffile:path];}2>> calls the definedUiscrollview, implement Proxy-(void) _loadsubviews{Uiscrollview* scrollview=[[UiscrollviewAlloc]initwithframe: Self. View. Bounds]; ScrollView. Polldatasource= Self; ScrollView. Polldelegate= Self; Self. ScrollView=scrollview; [ Self. ViewAddsubview:scrollview]; }3>> Agent Method#pragma mark-polldatasource//Returns the total number of columns- (Nsinteger) Numberofcolums: (Uiscrollview*) jrscroll{return 3;}//Returns the total number of cells- (Nsinteger) Numberofcells: (Uiscrollview*) jrscroll{return  Self. DataArray. Count;}//Returns the actual width height of each cell- (cgsize) Sizeofcellinscroll: (Uiscrollview*) Jrscroll Cellatindex: (Nsinteger) index{nsdictionary* temdic= Self. DataArray[index]; Self. Width=[temdic[@"W"] Floatvalue]; Self. Height=[temdic[@"H"] Floatvalue];returnCgsizemake ( Self. Width, Self. Height);}//Return to Cell view- (UIView*) Jrscroll: (Uiscrollview*) Jrscroll Cellatindex: (Nsinteger) index{Static NSString* [Email protected]"MyCell";UIView* Cell=[jrscroll Dequeuereusecellwithidenty:identy];//Create a cell that can be recycled repeatedly    if(cell==Nil) {cell=[[UIViewAlloc]initwithreuseidentifier:identy];//NSLog (@ "====>%li", index);}nsdictionary* dic= Self. DataArray[index]; Cell. IMGSRC=dic[@"img"]; Cell. Textlabel. Text=dic[@"Price"];returnCell;}#pragma mark-polldelegate//Return line spacing- (CGFloat) Rowofscroll: (Uiscrollview*) Jrscroll Spaceforcellwithflag: (pollscrollspace) jrspace{Switch(Jrspace) { CaseTopscrollspace:return  -; CaseBottomscrollspace:return Ten; CaseLeftscrollspace:return Ten; CaseRightscrollspace:return Ten; CaseRowscrollspace:return Ten; CaseColumnscrollspace:return Ten;default:return Ten; }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Reusable Waterfall Stream View

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.