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
相关文章推荐
- iOS-文件断点续传
- iOS开发中通过点击按钮异步加载图片
- ios-线程间的通信之下载图片
- iOS中 沙盒(sandbox)与归档(NSKeyedArchiver)
- iOS中NSThread(主线程,子线程)
- iOS NSString utf-8 Unicode互转
- iOS中的串行,并行,分组,一次,障碍,延迟,反复执行(GCD)
- 蓝懿iOS培训日志15 tableView和自定义Cell(1)
- iOS 9, Xcode 7,免费使用真机测试
- iOS中的动画
- iOS简单的数据持久化
- iOS的沙盒机制
- iOS - 邮件 短信 通讯录
- iOS提示框,为什么你应该使用 MBProgressHUD?
- iOS提示框,为什么你应该使用 MBProgressHUD?
- iOS - 数据持久化 - 文件的写入(简单对象和复杂对象)
- iOS上的ReactiveCocoa响应式编程
- iOS 去除tableview的黏性
- iOS中Bitcode的介绍及配置
- 用CocoaPods做iOS程序的依赖管理(转载)