UICollectionView

2017年3月14日
一.用UICollectionView實現(xiàn)如下答題界面(一頁一道習題)
1.效果:


Paste_Image.png

2.實現(xiàn)

//測試題目底部按鈕
#define bottom_btn_width (HHBWIDTH/2.0)
#define bottom_btn_height 49
#define practicePage_bgView_height 4
#define practicePage_contentView_uMargin 11
#define practicePage_contentView_yPos (practicePage_contentView_uMargin + practicePage_bgView_height)
#define practicePage_contentView_height (HHBHEIGHT - bottom_btn_height - top_nav_bar_height - prccticePage_contentView_yPos) //測試題庫

//  HuTestPracticeViewController.h
@interface HuTestPracticeViewController : HuViewController

#import "HuTestPracticeViewController.h"
@interface HuTestPracticeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    NSInteger _curId; //當前第幾題
    NSUInteger _totalNum; //總共習題數(shù)目
}
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *collectionViewFlowLayout;
@property (nonatomic, strong) NSIndexPath *curIndexPath; //當前的位置
@end

- (void)viewDidLoad {
    [super viewDidLoad];
    [self intView];
}
- (void)initView
{
     [self initColletionView];
}

//第一步:初始化。設(shè)置collectionView相關(guān)(試題)
- (void)initColletionView
{
    CGFloat viewWidth = HHBWIDTH;
    CGFloat viewHeight =  HHBHEIGHT - bottom_btn_height - top_nav_bar_height - practicePage_contentView_yPos;//[HuExerciseCollectionViewCell cellHeight];
    if (_collectionViewFlowLayout == nil) {
        self.collectionViewFlowLayout = [[UICollectionViewFlowLayout alloc] init];
        _collectionViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;//水平滾到
        _collectionViewFlowLayout.itemSize = CGSizeMake(viewWidth, viewHeight);
    }

    if (_collectionView == nil) {
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, practicePage_contentView_yPos, viewWidth, viewHeight) collectionViewLayout:_collectionViewFlowLayout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.pagingEnabled = YES;
        _collectionView.bounces = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[HuExerciseCollectionViewCell class] forCellWithReuseIdentifier:[HuExerciseCollectionViewCell identifier]];
    }

    [self.view addSubview:_collectionView];
}

//第二步:數(shù)據(jù)回來后更新 UICollectionView。
- (void)updateAllViewWithAnalyseData
{
    _totalNum = [_resExercisesM.resExercises count];
    [self updateViewSurfaceWithCurId:_curId];
    [_collectionView reloadData];
}

//第三步:設(shè)置collection和scrollview相關(guān)delegate
#pragma mark - collection Delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _totalNum;
}

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

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

    HuExerciseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[HuExerciseCollectionViewCell identifier] forIndexPath:indexPath];
    cell.delegate = self;
    NSInteger row = indexPath.row;

    if (_pageType == HuTestPracticePageTypeDoPaper) {
        //根據(jù)題號取得答案
        cell.userAnswer = [self getAnswerWithId:self.exercisesM.exercises[row].ID].mutableCopy;
        cell.exerciseM = self.exercisesM.exercises[row];
    }else if (_pageType == HuTestPracticePageTypeAnalyse){
        cell.resExerciseM = _resExercisesM.resExercises[row];
        cell.cellType = HuTestPracticePageTypeAnalyse;
    }

    return cell;
}

#pragma mark - scrollview
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGPoint pInView = [self.view convertPoint:self.collectionView.center toView:self.collectionView];
    self.curIndexPath = [self.collectionView indexPathForItemAtPoint:pInView];

    //如果不是最左邊在外做滑動,就需要更新相關(guān)界面
    if (_curId != 0  || _curIndexPath.row != 0)
    {
        _curId = _curIndexPath.row;
        [self updateViewSurfaceWithCurId:_curId];
    }

}

//第四步:創(chuàng)建 HuExerciseCollectionViewCell
//  HuExerciseCollectionViewCell.h
@interface HuExerciseCollectionViewCell : UICollectionViewCell
/**
 返回cell標識
 @return id
 */
+ (NSString *)identifier;

/**
 返回cell高度
 @return 高度
 */
+ (CGFloat)cellHeight;

@end

//  HuExerciseCollectionViewCell.m (我是用tableview創(chuàng)建的此處就省略tableview相關(guān)代碼了)
#import "HuExerciseCollectionViewCell.h"
@interface HuExerciseCollectionViewCell ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation HuExerciseCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self initData];
        [self initView];
    }
    return self;
}
+ (NSString *)identifier
{
    return @"HuExerciseCollectionViewCell";
}

+ (CGFloat)cellHeight
{
    return exercise_height;//最大默認值是這個,其實是根據(jù)選項個數(shù)重新修改
}

@end

2017年3月3日
1.默認調(diào)到到第幾個cv上

//跳轉(zhuǎn)到指定位置
NSInteger row = (_curId < 0) ? 0 : _curId;

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
[_collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:(UICollectionViewScrollPositionNone) animated:YES];

如果您發(fā)現(xiàn)本文對你有所幫助,如果您認為其他人也可能受益,請把它分享出去。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容