您的位置:首页 > 移动开发 > IOS开发

iOS中的瀑布流(RootCollectionViewControlle)

2015-09-17 20:01 253 查看
用了三个第三方




-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{
self.window=[[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]]autorelease];
//Overridepointforcustomizationafterapplicationlaunch.

//创建一个负责布局的对象
UICollectionViewWaterfallLayout*waterFlow=[[UICollectionViewWaterfallLayoutalloc]init];

//1.设置每个Cell的宽度
waterFlow.itemWidth=([UIScreenmainScreen].bounds.size.width-30)/2;
//2.设置分区的边框范围
waterFlow.sectionInset=UIEdgeInsetsMake(10,10,10,10);
//3.设置行和列的间距
waterFlow.minLineSpacing=10;

RootCollectionViewController*rootVC=[[RootCollectionViewControlleralloc]initWithCollectionViewLayout:waterFlow];

UINavigationController*navVC=[[UINavigationControlleralloc]initWithRootViewController:rootVC];

//4.设置代理
waterFlow.delegate=rootVC;

self.window.rootViewController=rootVC;
[rootVCrelease];
[navVCrelease];
[waterFlowrelease];

self.window.backgroundColor=[UIColorwhiteColor];
[self.windowmakeKeyAndVisible];
returnYES;
}


RootCollectionViewController.h服从代理

#import<UIKit/UIKit.h>
#import"UICollectionViewWaterfallLayout.h"
@interfaceRootCollectionViewController:UICollectionViewController<UICollectionViewDelegateWaterfallLayout>

@end


#import"RootCollectionViewController.h"
#import"GoodFoodCell.h"
#import"AFNetworking.h"
#import"GondFoot.h"
#import"UIImageView+WebCache.h"

#definekWidth_Cell_iamgeView([UIScreenmainScreen].bounds.size.width-30)/2

@interfaceRootCollectionViewController()
@property(nonatomic,retain)NSMutableArray*dataSource;//数据源
@end

@implementationRootCollectionViewController

-(void)dealloc{
self.dataSource=nil;
[superdealloc];
}

-(NSMutableArray*)dataSource{
if(!_dataSource){
self.dataSource=[NSMutableArrayarrayWithCapacity:1];
}
return[[_dataSourceretain]autorelease];
}

staticNSString*constreuseIdentifier=@"Cell";

-(void)viewDidLoad{
[superviewDidLoad];

//Uncommentthefollowinglinetopreserveselectionbetweenpresentations
//self.clearsSelectionOnViewWillAppear=NO;

//Registercellclasses
[self.collectionViewregisterClass:[GoodFoodCellclass]forCellWithReuseIdentifier:reuseIdentifier];

//Doanyadditionalsetupafterloadingtheview.
[selfgetData];
}

-(void)didReceiveMemoryWarning{
[superdidReceiveMemoryWarning];
//Disposeofanyresourcesthatcanberecreated.
}

//pragmamark--数据处理
//进行网络请求
-(void)getData{
AFHTTPRequestOperationManager*manger=[AFHTTPRequestOperationManagermanager];
[mangerGET:@"http://api.meishixing.com/picture/picturelist/last/page=1&city_id=13&session_id=000012ccc15955056ce39798d33df97a40f1cc"parameters:nilsuccess:^(AFHTTPRequestOperation*operation,idresponseObject){
//数据处理
[selfdataMangerFromDictionaay:responseObject];
}failure:^(AFHTTPRequestOperation*operation,NSError*error){

}];
}

//数据处理
-(void)dataMangerFromDictionaay:(NSDictionary*)datadic{
NSArray*array=datadic[@"result"];
for(NSDictionary*tempDicinarray){
//将tempDic封装成OC对象
GondFoot*food=[[GondFootalloc]init];
//使用KVC进行赋值
[foodsetValuesForKeysWithDictionary:tempDic];
//将封装好的Model添加到数据源
[self.dataSourceaddObject:food];

}
//刷新数据
[self.collectionViewreloadData];
}

//每个cell高度代理
-(CGFloat)collectionView:(UICollectionView*)collectionView
layout:(UICollectionViewWaterfallLayout*)collectionViewLayout
heightForItemAtIndexPath:(NSIndexPath*)indexPath{
#warning假数据
//取出对应的Model数据
GondFoot*food=self.dataSource[indexPath.item];

returnkWidth_Cell_iamgeView*food.image_height.floatValue/food.image_width.floatValue;

//return100;
}

/*
#pragmamark-Navigation

//Inastoryboard-basedapplication,youwilloftenwanttodoalittlepreparationbeforenavigation
-(void)prepareForSegue:(UIStoryboardSegue*)seguesender:(id)sender{
//Getthenewviewcontrollerusing[seguedestinationViewController].
//Passtheselectedobjecttothenewviewcontroller.
}
*/

#pragmamark<UICollectionViewDataSource>

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView{
#warningIncompletemethodimplementation--Returnthenumberofsections
return1;
}

-(NSInteger)collectionView:(UICollectionView*)collectionViewnumberOfItemsInSection:(NSInteger)section{
returnself.dataSource.count;
}

-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionViewcellForItemAtIndexPath:(NSIndexPath*)indexPath{
GoodFoodCell*cell=[collectionViewdequeueReusableCellWithReuseIdentifier:reuseIdentifierforIndexPath:indexPath];

//Configurethecell

GondFoot*food=self.dataSource[indexPath.item];

[cell.imageViewsd_setImageWithURL:[NSURLURLWithString:food.picture_url]placeholderImage:nil];

returncell;
}

@end









#import"GoodFoodCell.h"

@implementationGoodFoodCell

-(instancetype)initWithFrame:(CGRect)frame{
self=[superinitWithFrame:frame];
if(self){
[self.contentViewaddSubview:self.imageView];
}
returnself;
}

-(UIImageView*)imageView{
if(!_imageView){
self.imageView=[[[UIImageViewalloc]initWithFrame:self.bounds]autorelease];
}
return[[_imageViewretain]autorelease];
}

-(void)layoutSubviews{
[superlayoutSubviews];
self.imageView.frame=self.bounds;
}

-(void)dealloc{
self.imageView=nil;
[superdealloc];
}
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: