PDF 课件和源代码下载地址:
作者回复: 我这边新建立了个工程,照着15分50秒的代码重写了一遍,没发现问题。方便上传下你的代码么,我帮看一下。另外我再粘贴一遍我写的你也可以看看哪里不同。@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
collectionView.delegate = self;
collectionView.dataSource = self;
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
[self.view addSubview:collectionView];
}
#pragma mark -
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 20;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
return cell;
}
作者回复: 能上传下代码么? 我帮看下
作者回复: 有可能,跟着代码自动补全容易写成UICollectionViewLayout
作者回复: 是的 section只有一个的话,那么section一只是0. 而row和item本质上其实是一样的。那么为什么有两个呢,在视频中也简单介绍了,row是针对tableview的,到了collectionView,每一行可以有多个item布局,所以使用row不是特别到合适,所以整体到函数名都切换到了item
作者回复: 理论上是自己随便填,他的目的是为了在tableView内部进行cell重用的时候,区别不同的cell Class,让同种id的cell才能进行复用,达到最大的效率。当然作为key,不管针对什么样的业务逻辑考虑,还是都有一些习惯的,比如合理的命名,以及一些语言层面的优化等。