UICollectionView

2017年3月14日
一.用UICollectionView实现如下答题界面(一页一道习题)
1.效果:


UICollectionView_第1张图片
Paste_Image.png

2.实现

//测试题目底部按钮
#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 ()
{
    NSInteger _curId; //当前第几题
    NSUInteger _totalNum; //总共习题数目
}
@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];
}

//第一步:初始化。设置collectionView相关(试题)
- (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];
}

//第二步:数据回来后更新 UICollectionView。
- (void)updateAllViewWithAnalyseData
{
    _totalNum = [_resExercisesM.resExercises count];
    [self updateViewSurfaceWithCurId:_curId];
    [_collectionView reloadData];
}

//第三步:设置collection和scrollview相关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) {
        //根据题号取得答案
        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];

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

}

//第四步:创建 HuExerciseCollectionViewCell
//  HuExerciseCollectionViewCell.h
@interface HuExerciseCollectionViewCell : UICollectionViewCell
/**
 返回cell标识
 @return id
 */
+ (NSString *)identifier;

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

@end

//  HuExerciseCollectionViewCell.m (我是用tableview创建的此处就省略tableview相关代码了)
#import "HuExerciseCollectionViewCell.h"
@interface HuExerciseCollectionViewCell ()
@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;//最大默认值是这个,其实是根据选项个数重新修改
}

@end

2017年3月3日
1.默认调到到第几个cv上

//跳转到指定位置
NSInteger row = (_curId < 0) ? 0 : _curId;

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

如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。

你可能感兴趣的:(UICollectionView)