iOS中的瀑布流(RootCollectionViewControlle)

用了三个第三方

- (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
原文地址:https://www.cnblogs.com/wohaoxue/p/4817525.html