iOS開發 • 實例——用CollectionView和tableView實現作題界面功能

我獨立開發的 融易點,是一款爲高校學生金融知識學習作服務的app。那裏面邏輯比較複雜的界面,就是作題界面,有單選、多選題、判斷題、簡答題等等,相似的demo,不多,我也參考了一些,學到了很多經驗,值得寫下來分享,這個項目初版功能相對簡單,因此我仍是採用了MVC的架構,咱們先看看效果,以下所示:數組

1.gif

簡單說說實現的界面層次結構: ViewController中,主要的有:self.view->UICollectionView->UITableView
固然了,界面上還有UIImageView、UIProgressView、UIButton,這些是次要的。 接下來,開始具體的實現過程,首先,自定義選項的cell,我使用的是xib,同時建立HomePowerExamModel,如圖所示:bash

image.png

image.png

image.png

建立3個tableView,我把他們命名爲:HomeBaseExamTV,HomeSingleTV,HomeMultiTV,讓後面兩個繼承於第一個,先看看HomeBaseExamTV的代碼:架構

#import <UIKit/UIKit.h>
@class HomePowerExamModel;
@interface HomeBaseExamTV : UITableView<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) HomePowerExamModel * ExModel;
@property(nonatomic,strong) NSArray *answerList;
@property(nonatomic,strong) NSArray *questionList;
// 答案字典
@property(nonatomic,copy) NSDictionary *answer;
//  默認選中的答案
@property (nonatomic,copy) NSString *tempAnswer;

/**
 通用設置  子類實現必須調用super
 @param model      數據模型
 @param index      第幾題
 */
- (void)config:(HomePowerExamModel *)model index:(NSInteger)index;
@end
複製代碼
#import "HomeBaseExamTV.h"
#import "HomePowerExamModel.h"

@interface HomeBaseExamTV()

@end

@implementation HomeBaseExamTV
- (NSArray *)answerList{
    if (!_answerList) {
        _answerList = @[@"A",@"B",@"C",@"D",@"E"];
    }
    return _answerList;
}

- (NSArray *)questionList{
    if (!_questionList) {
        _questionList =  @[NONullString(_ExModel.QB_A),NONullString(_ExModel.QB_B),NONullString(_ExModel.QB_C),NONullString(_ExModel.QB_D),NONullString(_ExModel.QB_E)];
    }
    return _questionList;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return self;
}

- (void)config:(HomePowerExamModel *)model index:(NSInteger)index
{
    self.ExModel = model;
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 0)];
    UILabel * titleLb;
    /*題目文字*/
    if (iphoneSE) {
        titleLb = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, self.width-15, 0)];
    } else if (iphone7)
    {
        titleLb = [[UILabel alloc] initWithFrame:CGRectMake(10, 25, self.width-15, 0)];
    } else
    {
        titleLb = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, self.width-20, 0)];
    }
    
    titleLb.numberOfLines = 0;
    titleLb.text = [NSString stringWithFormat:@"%zd、%@",index+1,model.QB_Description];
    if (iphoneSE) {
        titleLb.font = [UIFont systemFontOfSize:14];
    } else
    {
        titleLb.font = [UIFont systemFontOfSize:15];
    }
    [titleLb sizeToFit];
    titleLb.textColor = [UIColor colorWithHexColorString:@"#3c4a55" alpha:1.0];
    titleLb.textAlignment = NSTextAlignmentNatural;
    [headerView addSubview:titleLb];
    headerView.height = titleLb.bottom + 10;
    self.tableHeaderView = headerView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return nil;
}
複製代碼

接下來看看HomeSingleTV的代碼,因爲判斷題也屬於單選題,不同的是,單選題最多有5個選項,判斷題只有兩個選項,後臺有返回一個type判斷題型,因此共用一個tableView,以下代碼所示:app

#import "HomeBaseExamTV.h"

@interface HomeSingleTV : HomeBaseExamTV

@end
複製代碼
#import "HomeSingleTV.h"
#import "HomeSelectionCell.h"
#import "HomePowerExamModel.h"

@interface HomeSingleTV()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) HomePowerExamModel  *model;
@property (nonatomic,strong) NSMutableDictionary  *rowHeightDic;
@property(nonatomic,assign) NSInteger selected;
@end

@implementation HomeSingleTV

- (NSMutableDictionary *)rowHeightDic
{
    if (!_rowHeightDic) {
        _rowHeightDic = [NSMutableDictionary dictionary];
    }
    return _rowHeightDic;
}

- (NSDictionary *)answer{
    
    if (self.selected != -1)
    {
        return  @{@"QuestionBID":@([self.model.QuestionBId intValue]),@"answer":self.answerList[_selected]};
    } else
    {
         return @{@"QuestionBID":@([self.model.QuestionBId intValue]),@"answer":@""};
    }
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.selected = -1;
        [self registerNib:[UINib nibWithNibName:@"HomeSelectionCell" bundle:nil] forCellReuseIdentifier:@"HomeSelectionCell"];
        self.backgroundColor = [UIColor clearColor];
        self.bounces = NO;
    }
    return self;
}

- (void)config:(HomePowerExamModel *)model index:(NSInteger)index{
    [super config:model index:index];//調用父類方法  設置通用設置
    self.model = model;
    self.delegate = self;
    self.dataSource = self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([self.model.QB_Type isEqualToString:@"3"]) {
        return 2;
    }
    else
    {
        return self.questionList.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeSelectionCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeSelectionCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.contentLb.text = [NSString stringWithFormat:@"%@",self.questionList[indexPath.row]];
    cell.contentLb.textColor = [UIColor colorWithHexColorString:@"#666666" alpha:1.0];
    cell.contentLb.font = [UIFont systemFontOfSize:14];
    cell.backgroundColor = [UIColor clearColor];
    NSArray * normalImgName = @[@"A1",@"B1",@"C1",@"D1",@"E1"];
    NSArray * selectImgName = @[@"A2",@"B2",@"C2",@"D2",@"E2"];
    [cell.selectBtn setImage:ImageNamed(normalImgName[indexPath.row]) forState:UIControlStateNormal];
    [cell.selectBtn setImage:ImageNamed(selectImgName[indexPath.row]) forState:UIControlStateSelected];
    if ([cell.contentLb.text isEqualToString:@""]) {
        cell.selectBtn.hidden = YES;
    } else
    {
        cell.selectBtn.hidden = NO;
    }
    
    cell.backgroundColor = [UIColor clearColor];
    if (indexPath.row == self.selected)
    {
        cell.selectBtn.selected = YES;
        
    } else
    {
        cell.selectBtn.selected = NO;
    }
    cell.selectBtn.tag = indexPath.row;
    [cell.selectBtn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    CGFloat height = [self getHeightWithStr:cell.contentLb.text];
    if (iphoneSE)
    {
        cell.bgView.frame = CGRectMake(0, 0, 270, height);
        cell.selectBtn.frame = CGRectMake(10, 2, 55, height);
        cell.contentLb.frame = CGRectMake(50, 2, 225, height);
    }
    else if (iphone7)
    {
        cell.bgView.frame = CGRectMake(0, 0, 275, height);
        cell.selectBtn.frame = CGRectMake(10, 2, 55, height);
        cell.contentLb.frame = CGRectMake(50, 2, 225, height);
    }
    else
    {
        cell.bgView.frame = CGRectMake(0, 0, 275, height);
        cell.selectBtn.frame = CGRectMake(15, 2, 55, height);
        cell.contentLb.frame = CGRectMake(50, 2, 225, height);
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selected = indexPath.row;
    [self reloadData];
}

/*根據返回的選項文字長度動態計算cell高度*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSNumber *number = [self.rowHeightDic objectForKey:@(indexPath.row).description];
    if (number) {
        if (iphoneSE) {
            return number.floatValue;
        }
        else
        {
            return number.floatValue + 10;
        }
    }else{
        CGFloat height = [self getHeightWithStr:self.questionList[indexPath.row]];
        [self.rowHeightDic setValue:[NSNumber numberWithFloat:height] forKey:@(indexPath.row).description];
        return height;
    }
}

- (CGFloat)getHeightWithStr:(NSString *)str{
    return  [self calculateStringHeight:str width:SCREEN_WIDTH - 10 -18 -5 -10 fontSize:17] + 15;
}

//  計算文字高度
- (CGFloat)calculateStringHeight:(NSString *)str width:(CGFloat)width fontSize:(CGFloat)size{
    
    return  [str boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:size]} context:nil].size.height;
    
}

/*保存已答過的答案*/
- (void)setTempAnswer:(NSString *)tempAnswer{
    if (tempAnswer.length == 0)
        return;
    _selected = [self.answerList indexOfObject:tempAnswer];
    [self reloadData];
}

/*單選點擊選項*/
- (void)selectBtnClick:(UIButton *)btn
{
    self.selected = btn.tag;
    [self reloadData];
}
@end
複製代碼

接着是HomeMultiTV的代碼,多選題因爲有多種組合,因此須要作兩種處理:1、對多選題答案進行快速排序,我是用數組來保存答案,因此對數組進行快速排序; 2、多選題在沒有提交答案的時候,選項是能夠從新選,經過按鈕來實現。 完整代碼以下:iphone

#import "HomeBaseExamTV.h"

@interface HomeMultiTV : HomeBaseExamTV

@end
複製代碼
#import "HomeMultiTV.h"
#import "HomeSelectionCell.h"
#import "HomePowerExamModel.h"

@interface HomeMultiTV ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) NSMutableDictionary *rowHeightDic;
@property(nonatomic,strong) NSMutableArray *selectedArray;
@property (nonatomic, strong) NSArray * multiAnswerList;

@end

@implementation HomeMultiTV
- (NSMutableDictionary *)rowHeightDic{
    if (!_rowHeightDic) {
        _rowHeightDic = [NSMutableDictionary dictionary];
    }
    return _rowHeightDic;
}

- (NSArray *)multiAnswerList
{
    if (!_multiAnswerList) {
        _multiAnswerList = @[@"A,",@"B,",@"C,",@"D,",@"E,"];
    }
    return _multiAnswerList;
}

/*後臺對多選題的答案要求按照ABCDE這樣的順序,因此進行排序*/
- (NSDictionary *)answer{
    
    NSString *answer = @"";
    NSString *Answer = @"";
    /*給數組快速排序*/
    [self.selectedArray sortUsingSelector:@selector(compare:)];
    for (NSNumber *number in self.selectedArray) {
        Answer =  [Answer stringByAppendingString:self.multiAnswerList[number.intValue]];
    }
    if (Answer.length > 0) {
        answer = [Answer substringToIndex:[Answer length]-1];
    }
    
    if (answer.length>0)
    {
        return @{@"QuestionBID":@([self.ExModel.QuestionBId intValue]),@"answer":answer};
    } else
    {
        return @{@"QuestionBID":@([self.ExModel.QuestionBId intValue]),@"answer":@""};
    }
}

- (NSMutableArray *)selectedArray{
    if (!_selectedArray) {
        _selectedArray = [NSMutableArray array];
        
    }
    return _selectedArray;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self registerNib:[UINib nibWithNibName:@"HomeSelectionCell" bundle:nil] forCellReuseIdentifier:@"HomeSelectionCell"];
        self.backgroundColor = [UIColor clearColor];
        self.bounces = NO;
    }
    return self;
}

- (void)config:(HomePowerExamModel *)model index:(int)index
{
    [super config:model index:index];
    self.delegate = self;
    self.dataSource = self;
}

/*保存答案*/
- (void)setTempAnswer:(NSString *)tempAnswer{
    
    if (tempAnswer.length == 0)
        return;
    for(int i =0; i < [tempAnswer length]; i++)
    {
        NSString *str = [tempAnswer substringWithRange:NSMakeRange(i, 1)];
        if (![str isEqualToString:@","]) {
            NSInteger index =  [self.answerList indexOfObject:str];
            if (![self.selectedArray containsObject:@(index)])
            {
                [self.selectedArray addObject:@(index)];
            }
        }
    }
    
    [self reloadData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.questionList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeSelectionCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeSelectionCell"];
    cell.contentLb.text = [NSString stringWithFormat:@"%@",self.questionList[indexPath.row]];
    cell.contentLb.font = [UIFont systemFontOfSize:14];
    cell.contentLb.textColor = [UIColor colorWithHexColorString:@"#666666" alpha:1.0];
    NSArray * normalImgName = @[@"A1",@"B1",@"C1",@"D1",@"E1"];
    NSArray * selectImgName = @[@"A2",@"B2",@"C2",@"D2",@"E2"];
    [cell.selectBtn setImage:ImageNamed(normalImgName[indexPath.row]) forState:UIControlStateNormal];
    [cell.selectBtn setImage:ImageNamed(selectImgName[indexPath.row]) forState:UIControlStateSelected];
    
    if ([cell.contentLb.text isEqualToString:@""]) {
        cell.selectBtn.hidden = YES;
    }
    else
    {
        cell.selectBtn.hidden = NO;
    }
    
    if ([self.selectedArray containsObject:@(indexPath.row)])
    {
        cell.selectBtn.selected = YES;
    } else
    {
        cell.selectBtn.selected = NO;
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.selectBtn.tag = indexPath.row;
    [cell.selectBtn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    CGFloat height = [self _getHeightWithStr:cell.contentLb.text];
    if (iphoneSE)
    {
        cell.bgView.frame = CGRectMake(0, 0, 270, height);
        cell.selectBtn.frame = CGRectMake(10, 2, 55, height);
        cell.contentLb.frame = CGRectMake(50, 2, 225, height);
    }
    else if (iphone7)
    {
        cell.bgView.frame = CGRectMake(0, 0, 275, height);
        cell.selectBtn.frame = CGRectMake(10, 2, 55, height);
        cell.contentLb.frame = CGRectMake(50, 2, 225, height);
    }
    else
    {
        
        cell.bgView.frame = CGRectMake(0, 0, 275, height);
        cell.selectBtn.frame = CGRectMake(15, 2, 55, height);
        cell.contentLb.frame = CGRectMake(50, 2, 225, height);
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.selectedArray containsObject:@(indexPath.row)]) {
        [self.selectedArray removeObject:@(indexPath.row)];
    }else{
        [self.selectedArray addObject:@(indexPath.row)];
    }
    
    [self reloadData];
}

/*動態計算高度*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSNumber *number = [self.rowHeightDic objectForKey:@(indexPath.row).description];
    
    if (number)
    {
        if (iphoneSE) {
            return number.floatValue;
        }
        else
        {
            return number.floatValue + 10;
        }
    }else{
        CGFloat height = [self _getHeightWithStr:self.questionList[indexPath.row]];
        [self.rowHeightDic setValue:
        [NSNumber numberWithFloat:height] forKey:@(indexPath.row).description];
        return height;
    }
}

- (CGFloat)_getHeightWithStr:(NSString *)str{
    return  [self calculateStringHeight:str width:SCREEN_WIDTH - 10 -18 -5 -10 fontSize:17] + 15;
}

//  計算文字高度
- (CGFloat)calculateStringHeight:(NSString *)str width:(CGFloat)width fontSize:(CGFloat)size{
    
    return  [str boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:
NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading 
attributes:
@{NSFontAttributeName:[UIFont systemFontOfSize:size]} context:nil].size.height;
}

- (void)selectBtnClick:(UIButton *)btn
{
    if ([self.selectedArray containsObject:@(btn.tag)]) {
        [self.selectedArray removeObject:@(btn.tag)];
    }
    else
    {
        [self.selectedArray addObject:@(btn.tag)];
    }
    [self reloadData];
}
複製代碼

model和 View的代碼已經實現,最後,看看Controller中的主要代碼實現,首先,建立CollectionView:ide

- (void)setupCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    if (iphoneSE) {
        flowLayout.itemSize = CGSizeMake(260, SCREEN_HEIGHT-64);
    }
    else if (iphone7)
    {
        flowLayout.itemSize = CGSizeMake(306, SCREEN_HEIGHT-64);
    } else
    {
        flowLayout.itemSize = CGSizeMake(337, SCREEN_HEIGHT-64);
    }
    
    //肯定水平滑動方向
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    if (iphoneSE) {
        self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, 260, SCREEN_HEIGHT-150) collectionViewLayout:flowLayout];
        self.collectionView.center = CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
    } else if (iphone7)
    {
        self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, 306, SCREEN_HEIGHT-180) collectionViewLayout:flowLayout];
        self.collectionView.center = CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
    } else
    {
        self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, 337, SCREEN_HEIGHT-200) collectionViewLayout:flowLayout];
        self.collectionView.center = CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
    }
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.scrollEnabled = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.collectionView];
    // 首次調用
    self.collectionView.contentOffset = CGPointMake(self.currentIndex*SCREEN_WIDTH, 0);
}
複製代碼

緊接着,實現CollectionView的代理方法和數據源方法:oop

#pragma mark -- UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    self.ExamModel = self.dataSource[indexPath.row];
    HomeBaseExamTV * baseTv = nil;
    //題型 1單選 2多選 3判斷
    if ([self.ExamModel.QB_Type isEqualToString:@"1"])
    {
        baseTv = [[HomeSingleTV alloc] initWithFrame:CGRectZero];
        self.topicTypeLb.text = @"單選題";
    }
    else if ([self.ExamModel.QB_Type isEqualToString:@"2"])
    {
        baseTv = [[HomeMultiTV alloc] initWithFrame:CGRectZero];
        self.topicTypeLb.text = @"多選題";
    }
    else if ([self.ExamModel.QB_Type isEqualToString:@"3"])
    {
        baseTv = [[HomeSingleTV alloc] initWithFrame:CGRectZero];
        self.topicTypeLb.text = @"判斷題";
    }
    
    if (iphoneSE) {
        baseTv.frame = CGRectMake(0, 140, 250, self.collectionView.height-64);
    } else if (iphone7)
    {
        baseTv.frame = CGRectMake(0, 170, 306, self.collectionView.height-64);
    } else
    {
        baseTv.frame = CGRectMake(0, 170, 337, self.collectionView.height-64);
    }
    [cell.contentView addSubview:baseTv];
    
    /*1表示作過了,看題目*/
    if(self.doneType == 1)
    {
        [baseTv config:self.ExamModel index:indexPath.item];
        baseTv.tempAnswer = self.ExamModel.userAnswer;
    } else
    {
        [baseTv config:self.ExamModel index:indexPath.item];
        baseTv.tempAnswer = self.ExamModel.userAnswer;
    }
    return cell;
}
複製代碼

因爲作題界面是能夠返回上一道題的,因此須要保存當前題目的答案,用到的方法以下:學習

//單選題
/*保存已答過的答案*/
- (void)setTempAnswer:(NSString *)tempAnswer{
    if (tempAnswer.length == 0)
        return;
    _selected = [self.answerList indexOfObject:tempAnswer];
    [self reloadData];
}

//多選題
/*保存答案*/
- (void)setTempAnswer:(NSString *)tempAnswer{
    
    if (tempAnswer.length == 0)
        return;
    for(int i =0; i < [tempAnswer length]; i++)
    {
        NSString *str = [tempAnswer substringWithRange:NSMakeRange(i, 1)];
        if (![str isEqualToString:@","]) {
            NSInteger index =  [self.answerList indexOfObject:str];
            if (![self.selectedArray containsObject:@(index)])
            {
                [self.selectedArray addObject:@(index)];
            }
        }
    }
    [self reloadData];
}
複製代碼

到這裏,作題界面就完成了,剩下的就是一些邏輯細節功能的處理,有幾個功能實現,我也寫下來分享,都是最簡單直接的方式,能夠直接用。 第1、時分秒計時。ui

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self regularTime];
}

- (void)regularTime
{
    __weak typeof(self)weakSelf = self;
    /*計時秒數*/
    secondValue = 0;
    timer = [NSTimer timerWithTimeInterval:1.0f block:^{
        [weakSelf timerAction];
    } repeats:YES];
    //計算時分秒
    NSString *str_hour = [NSString stringWithFormat:@"%02d",secondValue/3600];//時
    NSString *str_minute = [NSString stringWithFormat:@"%02d",(secondValue%3600)/60];//分
    NSString *str_second = [NSString stringWithFormat:@"%02d",secondValue%60];//秒
    NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
    NSLog(@"time:%@",format_time);
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}

- (void)timerAction
{
    /*正計時*/
    secondValue++;
    NSString *str_hour = [NSString stringWithFormat:@"%02d",secondValue/3600];
    NSString *str_minute = [NSString stringWithFormat:@"%02d",(secondValue%3600)/60];
    NSString *str_second = [NSString stringWithFormat:@"%02d",secondValue%60];
    NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
    self.currentTimeLb.text = [NSString stringWithFormat:@"%@",format_time];
}

- (void)dealloc
{
    NSLog(@"%s",__func__);
}
複製代碼

**第2、防止暴力點擊。**點肯定按鈕提交答案時,爲了防止用戶暴力點擊,因此,點擊間隔時間小於1秒的,無效。atom

- (IBAction)submitBtnClick:(UIButton *)btn
{
    DLog(@" %s ",__FUNCTION__);
    [[self class] cancelPreviousPerformRequestsWithTarget:self
                                                 selector:@selector(handleEvent:)
                                                   object:btn];
    [self performSelector:@selector(handleEvent:) withObject:btn afterDelay:1.0];
}

- (void)handleEvent:(UIButton *)btn
{}
複製代碼

**第3、獲取當前題目答案。**點擊確認提交答案的同時,保存當前題目答案,而後滑動到下一題,我再滑回上一道題的時候,依然能夠展現上一道題的答案,self.currentIndex是指當前題目的頁碼。

#pragma mark - scrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger index = (scrollView.contentOffset.x+scrollView.frame.size.width*0.5)/scrollView.frame.size.width;
    if (index != self.currentIndex || index == self.defautIndex) {//頁面改變了
        // 這裏設置成-1  爲了index == self.defautIndex 失效
        self.defautIndex = -1;
        
        [self updateAnswers];
        
        self.currentIndex = index;
        DLog(@"index:%zd",index);
    }
}

#pragma mark 更新暫存的答案
- (void)updateAnswers{
    
    for (UICollectionViewCell *cell in [self.collectionView visibleCells]) {
        
        HomeBaseExamTV *tableView = cell.contentView.subviews[0];
        
        [self.dataSource enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(HomePowerExamModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if (model.QuestionBId == [NSString stringWithFormat:@"%@",tableView.answer[@"QuestionBID"]] ) {
                model.userAnswer = tableView.answer[@"answer"];
                if ([model.QB_Type isEqualToString:@"3"] && ![model.userAnswer isEqualToString:@""]) {
                    if ([model.userAnswer isEqualToString:@"A"]) {
                        model.userAnswer = @"對";
                    } else
                    {
                        model.userAnswer = @"錯";
                    }
                }
                DLog(@"答案:%@",model.userAnswer);
                if (self.currentIndex == self.dataSource.count-1)
                {
                    [self submitData:model.userAnswer Type:@"1"];
                } else
                {
                    [self submitData:model.userAnswer Type:@"0"];
                }
            }
        }];
    }
}
複製代碼
//若是是單選題,則對應的是這個方法
- (NSDictionary *)answer{
    
    if (self.selected != -1)
    {
        return  @{@"QuestionBID":@([self.model.QuestionBId intValue]),@"answer":self.answerList[_selected]};
    } else
    {
         return @{@"QuestionBID":@([self.model.QuestionBId intValue]),@"answer":@""};
    }
}

//若是是多選題,那answer對應的方法就是快速排序,就是前面MultiTV中的方法
- (NSDictionary *)answer{
    
    NSString *answer = @"";
    NSString *Answer = @"";
    /*給數組快速排序*/
    [self.selectedArray sortUsingSelector:@selector(compare:)];
    for (NSNumber *number in self.selectedArray) {
        Answer =  [Answer stringByAppendingString:self.multiAnswerList[number.intValue]];
    }
    if (Answer.length > 0) {
        answer = [Answer substringToIndex:[Answer length]-1];
    }
    
    if (answer.length>0)
    {
        return @{@"QuestionBID":@([self.ExModel.QuestionBId intValue]),@"answer":answer};
    } else
    {
        return @{@"QuestionBID":@([self.ExModel.QuestionBId intValue]),@"answer":@""};
    }
}
複製代碼
總結

app項目業務開發,更可能是涉及到交互邏輯設計,會有不少細節,這些都是經驗,對app開發人員來講,在實現的過程當中會遇到不少bug,但踩過來了就是經驗的提高,記錄下來,除了分享,也是對本身的回顧,但願本身可以不斷提高,一塊兒加油。

相關文章
相關標籤/搜索