• !!!
    2019-07-27
    我也一样,同样的代码一步步写下来,item没有显示

    作者回复: 我这边新建立了个工程,照着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;
    }

    
     1
  • 海纳百川
    2019-05-17
    同样的代码,没有显示item

    作者回复: 能上传下代码么? 我帮看下

     1
     1
  • RamboPan
    2019-08-06
    我感觉没显示的原因可能之一是:
    UICollectionViewLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    因为这里用了一次向上转型。
    我第一次写成了这种,所以没有布局。
    UICollectionViewLayout * layout = [[UICollectionViewLayout alloc] init];
    展开

    作者回复: 有可能,跟着代码自动补全容易写成UICollectionViewLayout

    
    
  • Geek_baozi
    2019-07-28
    朱老师好,我在collectionview中的didselectedItemAtIndexPath中打印了item,row,section,row和item的值是一样的,section总是为0

    作者回复: 是的 section只有一个的话,那么section一只是0. 而row和item本质上其实是一样的。那么为什么有两个呢,在视频中也简单介绍了,row是针对tableview的,到了collectionView,每一行可以有多个item布局,所以使用row不是特别到合适,所以整体到函数名都切换到了item

    
    
  • itgou
    2019-06-28
    forCellWithReuseIdentifier这个字符串参数 是自己随便写吗? 相当于是HTML中自己定义的id?

    作者回复: 理论上是自己随便填,他的目的是为了在tableView内部进行cell重用的时候,区别不同的cell Class,让同种id的cell才能进行复用,达到最大的效率。当然作为key,不管针对什么样的业务逻辑考虑,还是都有一些习惯的,比如合理的命名,以及一些语言层面的优化等。

    
    
  • 海纳百川
    2019-05-17
    老师可以在讲课中说一些开发中需要注意的地方吗,比如经常遇到的一些坑
    
    
我们在线,来聊聊吧