手寫CollectionView並調整佈局

創建

 if (!_colcView) 
        int colcViewSideLength = self.view.frame.size.width - cellGap;

        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        _colcView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, colcViewSideLength, colcViewSideLength) collectionViewLayout:flowLayout];
        _colcView.showsHorizontalScrollIndicator = NO;
        _colcView.showsVerticalScrollIndicator = NO;
        _colcView.delegate = self;
        _colcView.dataSource = self;
        _colcView.center = self.view.center;
        _colcView.backgroundColor = [UIColor clearColor];

        [_colcView registerClass:[EPLGuestureColcViewCell class] forCellWithReuseIdentifier:CellID];

        [self.view addSubview:_colcView];
    }

實現協議

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _arrBtnImg.count;
}

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

    EPLGuestureColcViewCell *colcCell = [collectionView dequeueReusableCellWithReuseIdentifier:CellID forIndexPath:indexPath];
    if (!colcCell) {
        colcCell = [[EPLGuestureColcViewCell alloc]initWithFrame:CGRectZero];
    }
    [colcCell sizeToFit];
    return colcCell;
}

-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    EPLGuestureColcViewCell *cell0 = (EPLGuestureColcViewCell*)cell;
    cell0.imgView.image = [UIImage imageNamed:_arrBtnImg[indexPath.item]];
    NSNumber *numValue;
    if (_arrBtnValues.count) {
        numValue = _arrBtnValues[indexPath.item];
    }else{
        numValue = [NSNumber numberWithInt:-1];
    }

    switch (numValue.intValue) {
        case -1:
            cell0.labTitle.text = NSLocalizedString(@"GUESTURE_BUTTON_CHOOSE_SHOORTKEY", nil);
            break;
        case 0:
            cell0.labTitle.text = NSLocalizedString(@"GUESTURESETTING_ROW_PHONE", nil);
            break;
        case 1:
            cell0.labTitle.text = NSLocalizedString(@"GUESTURESETTING_ROW_SMS", nil);
            break;
        case 2:
            cell0.labTitle.text = NSLocalizedString(@"GUESTURESETTING_ROW_RECORDER", nil);
            break;
        case 3:
            cell0.labTitle.text = NSLocalizedString(@"GUESTURESETTING_ROW_CAMERA", nil);
            break;
        default:
            break;
    }
}

#pragma mark -- UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];

    NSNumber *shortcutKey;
    if (_arrBtnValues.count) {
        shortcutKey = _arrBtnValues[indexPath.item];
    }else{
        shortcutKey = [NSNumber numberWithInt:-1];
    }

    EPLGuestureSettingController *settingController = [[EPLGuestureSettingController alloc]initWithStyle:UITableViewStyleGrouped];
    settingController.selectedIndex = shortcutKey.integerValue;
    settingController.currentShortcutKeyName = _arrBtnKeys[indexPath.item];
    settingController.shortcutKeyUpdated = ^(NSNumber *selectedIndex){

        [_arrBtnValues replaceObjectAtIndex:indexPath.item withObject:selectedIndex];

        NSDictionary *dicGuestureShortcutKeysValue = [NSDictionary dictionaryWithObjects:_arrBtnValues forKeys:_arrBtnKeys];

        [EPLDataOperateKit saveGuestureShortcutKeysValue:dicGuestureShortcutKeysValue];
        [_colcView reloadData];
    };

    [self.navigationController pushViewController:settingController animated:YES];
}


#pragma mark -

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

    int cellEdgeLength = (collectionView.frame.size.width-2*indicatorInsets-cellGap)/2;
    return CGSizeMake(cellEdgeLength, cellEdgeLength);
}

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

    if (isEliteDevice) {
        return UIEdgeInsetsMake(indicatorInsets, indicatorInsets, indicatorInsets, indicatorInsets);
    }else{
        int cellEdgeLength = (collectionView.frame.size.width-2*indicatorInsets-cellGap)/2;
        int indicatorInsetsForSigleItem = indicatorInsets + cellGap/2 + cellEdgeLength;

        return UIEdgeInsetsMake(indicatorInsetsForSigleItem/2, indicatorInsetsForSigleItem/2, indicatorInsetsForSigleItem/2, indicatorInsetsForSigleItem/2);
    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return cellGap;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return cellGap;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章