iOS collectionview简单用法

UICollectionViewDelegate, UICollectionViewDataSource实现了这两个代理

//collectionview相关

```

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:musicUrl] options:nil];

AVPlayerItem *songitem = [[AVPlayerItem alloc] initWithAsset:asset];

self.player = [[AVPlayer alloc] initWithPlayerItem:songitem];

[songitem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

    if ([keyPath isEqualToString:@"status"]) {

        AVPlayerItem *item = (AVPlayerItem *)object;

        //AVPlayerItemStatus *status = item.status;

        if (item.status == AVPlayerItemStatusReadyToPlay) {

            [self.player play];

            //对播放界面的一些操作,时间、进度等

        }

    }

}

```

记得在info设置NSAppTransportSecurity - NSAllowsArbitraryLoads - yes

AVAudioPlayer只可播放本地音频,不能播放网络音频

- (void)setCollectionviewLayout {

UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];

[flow setScrollDirection:UICollectionViewScrollDirectionVertical];//竖直滑动

//    flow.minimumLineSpacing = 0;

//    flow.minimumInteritemSpacing = 0;

flow.itemSize = CGSizeMake(75, 120);

self.typeCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(15, 0, Screen_Width-30, Screen_Height-49-64) collectionViewLayout:flow];

self.typeCollection.backgroundColor = [UIColor clearColor];

self.typeCollection.delegate = self;

self.typeCollection.dataSource = self;

self.typeCollection.showsHorizontalScrollIndicator = NO;

self.typeCollection.showsVerticalScrollIndicator = NO;

[self.typeCollection registerNib:[UINib nibWithNibName:@"TypeCell" bundle:nil] forCellWithReuseIdentifier:@"TypeCell"];//注册cell

[self.view addSubview:self.typeCollection];

[self.typeCollection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];//注册分区头

}


#pragma mark - collection

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

return 2;

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

//    return self.treeList.count;

return 9;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

TypeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TypeCell" forIndexPath:indexPath];

//    [cell setDateWithString:self.treeList[indexPath.row]];

return cell;

}

//返回分区header

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 30)];

label.text = @"header";

UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];

[view addSubview:label];

return view;

}

//分区header的大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {

return CGSizeMake(Screen_Width, 30);

}

你可能感兴趣的:(iOS collectionview简单用法)