Waterfall Stream in iOS (Rootcollectionviewcontrolle)

Source: Internet
Author: User
Tags uikit

Used three third-party

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {Self.window=[[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]] autorelease]; //Override point for customization after application launch. //create an object that is responsible for the layoutUicollectionviewwaterfalllayout *waterflow =[[Uicollectionviewwaterfalllayout alloc] init]; //1. Set the width of each cellWaterflow.itemwidth = ([UIScreen mainscreen].bounds.size.width- -) /2; //2. Set the border range of a partitionWaterflow.sectioninset = Uiedgeinsetsmake (Ten,Ten,Ten,Ten); //3. Set the spacing between rows and columnsWaterflow.minlinespacing =Ten; Rootcollectionviewcontroller*ROOTVC =[[Rootcollectionviewcontroller alloc] initwithcollectionviewlayout:waterflow]; Uinavigationcontroller*NAVVC =[[Uinavigationcontroller alloc] INITWITHROOTVIEWCONTROLLER:ROOTVC]; //4. Setting up the agentWaterflow.Delegate=ROOTVC; Self.window.rootViewController=ROOTVC; [ROOTVC release]; [NAVVC release]; [Waterflow release]; Self.window.backgroundColor=[Uicolor Whitecolor]; [Self.window makekeyandvisible]; returnYES;}

RootCollectionViewController.h obey the agent

#import <UIKit/UIKit.h>"UICollectionViewWaterfallLayout.h"@ Interface Rootcollectionviewcontroller:uicollectionviewcontroller< uicollectionviewdelegatewaterfalllayout>@end







#import"RootCollectionViewController.h"#import"GoodFoodCell.h"#import"AFNetworking.h"#import"GondFoot.h"#import"uiimageview+webcache.h"#defineKwidth_cell_iamgeview ([UIScreen mainscreen].bounds.size.width-30)/2@interface Rootcollectionviewcontroller () @property (nonatomic, retain) Nsmutablearray*datasource;//Data Source@end @implementation Rootcollectionviewcontroller- (void) dealloc{Self.datasource=Nil; [Super Dealloc];}-(Nsmutablearray *) datasource{if(!_datasource) {Self.datasource= [Nsmutablearray arraywithcapacity:1]; } return[[_datasource retain] autorelease];}StaticNSString *ConstReuseidentifier =@"Cell";- (void) viewdidload {[Super viewdidload]; //uncomment the following line to preserve selection between presentations//self.clearsselectiononviewwillappear = NO; //Register Cell Classes[Self.collectionview Registerclass:[goodfoodcellclass] Forcellwithreuseidentifier:reuseidentifier]; //Do any additional setup after loading the view.[self getData];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}//pragma mark--Data processing//make a network request- (void) getdata{Afhttprequestoperationmanager*manger=[Afhttprequestoperationmanager Manager]; [Manger GET:@"http://api.meishixing.com/picture/picturelist/last/page=1&city_id=13&session_id= 000012CCC15955056CE39798D33DF97A40F1CC"Parameters:nil success:^ (Afhttprequestoperation *operation, id responseobject) { //Data Processing[self datamangerfromdictionaay:responseobject]; } Failure:^ (Afhttprequestoperation *operation, Nserror *error) { }];}//Data Processing- (void) Datamangerfromdictionaay: (Nsdictionary *) datadic{Nsarray*array = datadic[@"result"]; for(Nsdictionary *tempdicinchArray) { //encapsulating tempdic into OC objectsGondfoot *food =[[Gondfoot alloc] init]; //use KVC to assign values[Food setvaluesforkeyswithdictionary:tempdic]; //Add the encapsulated Model to the data source[Self.datasource Addobject:food]; } //Refresh Data[Self.collectionview reloaddata];}//per cell height proxy-(CGFloat) CollectionView: (Uicollectionview *) CollectionView layout: (Uicollectionviewwaterfalllayout*) collectionviewlayout Heightforitematindexpath: (Nsindexpath*) indexpath{#warningFalse data//Take out the corresponding model dataGondfoot *food =Self.datasource[indexpath.item]; returnKwidth_cell_iamgeview * Food.image_height.floatValue/Food.image_width.floatValue; //return;}/*#pragma mark-navigation//in a storyboard-based application, you'll often want to do a little preparation before n avigation-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {//Get the new view controller using [s] Egue Destinationviewcontroller]. Pass the selected object to the new view controller.}*/#pragmaMark <uicollectionviewdatasource>-(Nsinteger) Numberofsectionsincollectionview: (UICollectionView *) CollectionView {#warningIncomplete method Implementation--Return the number of sectionsreturn 1;}-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (nsinteger) Section {returnSelf.dataSource.count;}-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath {Goodfoodcell*cell =[CollectionView dequeuereusablecellwithreuseidentifier:reuseidentifier Forindexpath:indexpath]; //Configure the cellGondfoot*food =Self.datasource[indexpath.item]; [Cell.imageview Sd_setimagewithurl:[nsurl URLWithString:food.picture_url] placeholderimage:nil]; returnCell;}

@end

#import"GoodFoodCell.h"@implementation Goodfoodcell-(Instancetype) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {[Self.contentview AddSubview:self.imageView]; }    returnSelf ;}-(Uiimageview *) imageview{if(!_imageview) {Self.imageview=[[[ Uiimageview alloc] initWithFrame:self.bounds] autorelease]; }    return[[_imageview retain] autorelease];}- (void) layoutsubviews{[Super Layoutsubviews]; Self.imageView.frame=self.bounds;}- (void) dealloc{Self.imageview=Nil; [Super Dealloc];} @end

Waterfall Stream in iOS (Rootcollectionviewcontrolle)

Related Article

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.