標籤:
用了三個第三方
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. //建立一個負責布局的對象 UICollectionViewWaterfallLayout *waterFlow = [[UICollectionViewWaterfallLayout alloc] init]; //1.設定每個Cell的寬度 waterFlow.itemWidth = ([UIScreen mainScreen].bounds.size.width - 30) / 2; //2.設定分區的邊框範圍 waterFlow.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10); //3.設定行和列的間距 waterFlow.minLineSpacing = 10; RootCollectionViewController *rootVC = [[RootCollectionViewController alloc] initWithCollectionViewLayout:waterFlow]; UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:rootVC]; //4.設定代理 waterFlow.delegate = rootVC; self.window.rootViewController = rootVC; [rootVC release]; [navVC release]; [waterFlow release]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES;}
RootCollectionViewController.h服從代理
#import <UIKit/UIKit.h>#import "UICollectionViewWaterfallLayout.h"@interface RootCollectionViewController : UICollectionViewController<UICollectionViewDelegateWaterfallLayout>@end
#import "RootCollectionViewController.h"#import "GoodFoodCell.h"#import "AFNetworking.h"#import "GondFoot.h"#import "UIImageView+WebCache.h"#define kWidth_Cell_iamgeView ([UIScreen mainScreen].bounds.size.width - 30) / 2@interface RootCollectionViewController ()@property (nonatomic, retain) NSMutableArray *dataSource; //資料來源@end@implementation RootCollectionViewController- (void)dealloc{ self.dataSource = nil; [super dealloc];}- (NSMutableArray *)dataSource{ if (!_dataSource) { self.dataSource = [NSMutableArray arrayWithCapacity:1]; } return [[_dataSource retain] autorelease];}static NSString * const reuseIdentifier = @"Cell";- (void)viewDidLoad { [super viewDidLoad]; // Uncomment the following line to preserve selection between presentations // self.clearsSelectionOnViewWillAppear = NO; // Register cell classes [self.collectionView registerClass:[GoodFoodCell class] forCellWithReuseIdentifier:reuseIdentifier]; // Do any additional setup after loading the view. [self getData];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}//pragma mark -- 資料處理//進行網路請求- (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) { //資料處理 [self dataMangerFromDictionaay:responseObject]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { }];}//資料處理- (void)dataMangerFromDictionaay:(NSDictionary *)datadic{ NSArray *array = datadic[@"result"]; for (NSDictionary *tempDic in array) { //將 tempDic 封裝成 OC對象 GondFoot *food = [[GondFoot alloc] init]; //使用KVC 進行賦值 [food setValuesForKeysWithDictionary:tempDic]; //將封裝好的 Model 添加到資料來源 [self.dataSource addObject:food]; } //重新整理資料 [self.collectionView reloadData];}//每個cell高度代理- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewWaterfallLayout *)collectionViewLayout heightForItemAtIndexPath:(NSIndexPath *)indexPath{#warning 假資料 //取出對應的Model 資料 GondFoot *food = self.dataSource[indexPath.item]; return kWidth_Cell_iamgeView * food.image_height.floatValue / food.image_width.floatValue; // return 100;}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.}*/#pragma mark <UICollectionViewDataSource>- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {#warning Incomplete method implementation -- Return the number of sections return 1;}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.dataSource.count;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { GoodFoodCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; // Configure the cell GondFoot *food = self.dataSource[indexPath.item]; [cell.imageView sd_setImageWithURL:[NSURL URLWithString:food.picture_url] placeholderImage:nil]; return cell;}
@end
#import "GoodFoodCell.h"@implementation GoodFoodCell- (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { [self.contentView addSubview:self.imageView]; } return self;}- (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
iOS中的瀑布流(RootCollectionViewControlle)