自己做项目 还是和之前那个一样 全程记录 目前是纯代码 为了扎实自己的技术
1.首页 UICollectionView
*对于 collectionView 和 tavbleView 的 不同
UICollection 是 UITableView 的升级版 支持左右 上下滑动
设计思想:
1.布局
UITableView布局 可以由自己完成 (自带 cell)和他的代理
UICollection布局 由 UICollectionViewFlowLayout 和 UICollectionViewDelegateFlowLayout 决定
2.数据源
UITableView 由UITableViewDataSourse 提供
UICollection 由UICollectionDataSourse 提供
3.布局样式
UITableView 多行单列
UICollection 多行多列
4.UITableViewCell 的不同
UITableView 自带 imageview; textlable; detail; assoryview (辅助视图)
UICollection 自带 contentView,contentView 什么都没有
5.cell 的使用
UITableviewCell 创建时直接使用 ,之后使用重用机制+注册
UICollectionCell 必须注册 才能使用 你可以选择自己再建个集成 collectionCell 的类 然后引入
6.
UITableView 支持编辑(增加,删除),移动
UICollectionView 不支持
7.共同点:
都可以滚动 继承 UIScrollView; 方向也是单一的 要么左右 要么上下 左右的适合 适用与我们手机的横屏
#import "HomeViewController.h"
#import "HomePageCell.h"
#define kHomeCell @"cell"
@interface HomeViewController ()<UICollectionViewDataSource>
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// self.view.backgroundColor = [UIColor whiteColor];
//因为UICollectionViewFlowLayout 是一个抽象的布局基类 所以需要用子类
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
//配置 collection 的时候 很多属性干嘛的 很乱啊 设置的 和自己想要的效果不一样
layout.minimumInteritemSpacing = 0;
// layout.sectionInset = UIEdgeInsetsMake(5, 0, 5, 0);
// layout.minimumLineSpacing = 0;
//以上两个 只有一个分区 所以没啥用
//item 的大小
layout.itemSize = CGSizeMake(320, 80);
//创建 collection
UICollectionView *homeColl = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
homeColl.backgroundColor = [UIColor whiteColor];
[self.view addSubview:homeColl];
//注册 cell collection 特别的注册 cell 的方法 不是 self 了
[homeColl registerClass:[HomePageCell class] forCellWithReuseIdentifier:kHomeCell];
//设置数据源代理
homeColl.dataSource = self;
[layout release];
[homeColl release];
//为什么运行之后 是黑色的 你没设颜色呗 二货
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 5;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
HomePageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kHomeCell forIndexPath:indexPath];
cell.backgroundColor = [UIColor magentaColor];
return cell;
}
注:一开始我的 label 和 textlabel 都是会随机动的 当我修改 item 的大小时 后来我给 collectionView 的 cell 加了个颜色 发现了 问题 我们在 cell 类里设置的 title 是在 cell 上面的 你修改 item 的大小 就会造成它乱动
他们的关系: collectionView–>cell–>title
2.做一个简单的数据解析 通过网页 json 后 可以发现接口 是什么构成的 建立 model 类
(我挺想试试单例 下面可能会做一个)
因为数据解析用的比较频繁 我们可以写成一个类 然后调用 很方便