开发ios已经有了一段时间了,各种空间也使用了很多,android开发其实也一直是没有落下的,对比ios与android来讲,安卓开发起来更加的灵活,只要你有想象力,android可以做更多的事情,但是ios的话,苹果已经在底层做了非常好的封装,开发者在使用的时候,更多的是在使用ios所封装的接口,总体上来将我个人觉得android是更加油前途的,运用也会更广泛。
其实android跟IOS开发的话,道理是相同的,比如,android的listview对比IOS的tabview,android的gridView对比IOS的CollectionView,前者主要是实现列表类的数据显示,后者主要实现分块的类型实现。我们先看下效果。
在这里我们使用uicollectionView去实现,并且做了一搬的屏幕适配。
1.创建项目,删除原来的storyboard,使用collectionview control,继承UIcollectionViewCOntroller。
2.使用autolayout进行单cell布局。
我们的要求是由一个image 。两个textlabel。
这里不得不提一句,IOS在自动布局上面比android好很多,androidstudio在2.3之后也加上了这么一种约束的功能,我个人使用起来的感觉不太好,后面需要再慢慢体验。
3.编写view并且连线,将我们的cell中的对象与view对应起来。
//
// HeroCell.h
// csdnuicollectionview实现列表
//
// Created by huhai on 2017/4/22.
// Copyright © 2017年 huhai. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface HeroCell : UICollectionViewCell
@property (weak,nonatomic) IBOutletUIImageView *icon;
@property (weak,nonatomic) IBOutletUILabel *name;
@property (weak,nonatomic) IBOutletUILabel *desc;
@end
4.建立model对应plist文件
#import <Foundation/Foundation.h>
@interface HeroModel : NSObject
@property (nonatomic,copy)NSString *name;
@property (nonatomic,copy)NSString *icon;
@property (nonatomic,copy)NSString *intro;
-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)heroModelWithDict:(NSDictionary *)dict;
@end
//
// HeroModel.m
// collectionView英雄实现
//
// Created by huhai on 2017/4/21.
// Copyright © 2017年 huhai. All rights reserved.
//
#import "HeroModel.h"
@implementation HeroModel
-(instancetype)initWithDict:(NSDictionary *)dict{
if (self==[superinit]) {
[selfsetValuesForKeysWithDictionary:dict];
}
returnself;
}
+(instancetype)heroModelWithDict:(NSDictionary *)dict{
return [[selfalloc]initWithDict:dict];
}
@end
5.懒加载数据(解析plist文件)
//
// HeroModel.m
// collectionView英雄实现
//
// Created by huhai on 2017/4/21.
// Copyright © 2017年 huhai. All rights reserved.
//
#import "HeroModel.h"
@implementation HeroModel
-(instancetype)initWithDict:(NSDictionary *)dict{
if (self==[superinit]) {
[selfsetValuesForKeysWithDictionary:dict];
}
returnself;
}
+(instancetype)heroModelWithDict:(NSDictionary *)dict{
return [[selfalloc]initWithDict:dict];
}
@end
6.实现代理方法(行,组,行内容,并且设置cell的宽高)
//组
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return1;
}
//行
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return_dataArray.count;
}
//内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
HeroCell *cell=[collectionViewdequeueReusableCellWithReuseIdentifier:idetiferforIndexPath:indexPath];
HeroModel *model=_dataArray[indexPath.item];
cell.heroModel=model;
//cell.backgroundColor=[UIColor redColor];
return cell;
}
到此差不多就完了,后面会给出详细的代码。
这篇文章没有讲的很详细,主要是公司要弄微信公众号,麻烦死了,弄的我,后面再补上缺陷
代码下载地址:
http://download.csdn.net/detail/qq_16177199/9828102