美文网首页
UICollectionView的知识点

UICollectionView的知识点

作者: 崔又淇 | 来源:发表于2016-07-08 16:22 被阅读49次

    #pragma mark - UICollectionViewDelegateFlowLayout

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0 || indexPath.section == 1) {

    return CGSizeMake(SCREEN_WIDTH, 80);

    } else if (indexPath.section == 2) {

    return CGSizeMake(SCREEN_WIDTH / 3 - 15, 200);

    } else if (indexPath.section == 3) {

    return CGSizeMake(SCREEN_WIDTH / 2 - 25, 100);

    } else {

    return CGSizeMake(SCREEN_WIDTH, 250);

    }

    }

    - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    if (section == 2) {

    return UIEdgeInsetsMake(5, 5, 5, 5);

    } else if (section == 3) {

    return UIEdgeInsetsMake(10, 10, 10, 10);

    } else {

    return UIEdgeInsetsMake(0, 0, 0, 0);

    }

    }

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

    if (section == 0) {

    return CGSizeMake(SCREEN_WIDTH, 1);

    } else if (section == 1) {

    return CGSizeMake(SCREEN_WIDTH, 15);

    } else {

    return CGSizeMake(SCREEN_WIDTH, 45);

    }

    }

    #pragma mark - UICollectionViewDataSource

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

    if (section == 0 || section == 1) {

    return 1;

    } else if (section == 2) {

    return 3;

    } else if (section == 3) {

    return 4;

    } else {

    return 2;

    }

    }

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

    if (indexPath.section == 0 || indexPath.section == 1) {

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

    NSDictionary *dic = _customArr[indexPath.section];

    cell.titleLabel.text = dic[@"title"];

    cell.subTitleLabel.text = dic[@"sub"];

    cell.picImageView.image = [UIImage imageNamed:dic[@"image"]];

    return cell;

    } else if (indexPath.section == 2) {

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

    // 本地数据

    SceneModel *scene = _sceneArr[indexPath.row];

    cell.sceneLabel.text = scene.journeyName;

    [cell.sceneImage sd_setImageWithURL:[NSURL URLWithString:scene.imageUrl] placeholderImage:[UIImage imageNamed:@"baobao"]];

    [cell.userImage sd_setImageWithURL:[NSURL URLWithString:scene.userAvatar] placeholderImage:[UIImage imageNamed:@"baobao"]];

    return cell;

    } else if (indexPath.section == 3){

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

    SelectModel *select = _selectionArr[indexPath.row];

    cell.selectLabel.text = select.title;

    [cell.selectPic sd_setImageWithURL:[NSURL URLWithString:select.imageUrl] placeholderImage:[UIImage imageNamed:@"baobao"]];

    return cell;

    } else {

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

    TravelModel *travel = _travelArr[indexPath.row];

    cell.digestTitleL.text = travel.title;

    cell.digestSubTitleL.text = travel.subTitle;

    cell.digestSeeCountL.text = travel.follow;

    cell.digestTimeL.text = travel.date;

    [cell.disgestBackImg sd_setImageWithURL:[NSURL URLWithString:travel.imageUrl] placeholderImage:[UIImage imageNamed:@"baobao"]];

    return cell;

    }

    }

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

    return 5;

    }

    下面这个方法是用来处理分区的,如果返回nil或者0程序会崩溃.很重要!!!

    //返回对应section里 头UICollectionReusableView和脚View

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

    if (indexPath.section == 0 || indexPath.section == 1) {

    FindDivisionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"FindDivisionReusableView" forIndexPath:indexPath];

    if (indexPath.section == 0) {

    reusableView.hidden = YES;

    return reusableView;

    } else {

    return reusableView;

    }

    } else {

    FindCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"FindCollectionReusableView" forIndexPath:indexPath];

    reusableView.sectionLabel.text = _sectionArr[indexPath.section - 2];

    return reusableView;

    }

    }

    #pragma mark - UICollectionViewDelegate

    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0) {

    RouteViewController *routeVc = [[RouteViewController alloc] initWithNibName:NSStringFromClass([RouteViewController class]) bundle:nil];

    [self.navigationController pushViewController:routeVc animated:YES];

    } else if (indexPath.section == 1){

    } else if (indexPath.section == 2){

    } else if (indexPath.section == 3) {

    } else {

    }

    }

    相关文章

      网友评论

          本文标题:UICollectionView的知识点

          本文链接:https://www.haomeiwen.com/subject/hdkrjttx.html