iOS中的瀑布流(RootCollectionViewControlle)

來源:互聯網
上載者:User

標籤:

用了三個第三方

- (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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.