ICRQSingleSelectViewController.m 3.94 KB
Newer Older
mei's avatar
mei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
//
//  ICRQSingleSelectViewController.m
//  XFFruit
//
//  Created by Xummer on 6/7/15.
//  Copyright (c) 2015 Xummer. All rights reserved.
//

#import "ICRQSingleSelectViewController.h"
#import "ICRQuestionManager.h"

static NSString *kCellID = @"cellID";

@interface ICRQSingleSelectViewController ()
<
    UITableViewDataSource,
    UITableViewDelegate
>

@property (strong, nonatomic) IBTTableView *m_tableView;
@property (strong, nonatomic) NSIndexPath *m_currentIndexP;

@end

@implementation ICRQSingleSelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    for (NSDictionary *dict in self.m_answer.details) {
        NSIndexPath *indexP =
        [NSIndexPath indexPathForRow:[dict[ @"answer" ] unsignedIntegerValue] inSection:0];
        self.m_currentIndexP = indexP;
        [_m_tableView selectRowAtIndexPath:indexP animated:NO scrollPosition:UITableViewScrollPositionNone];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Private Method

- (void)initScrollViewWithRect:(CGRect)rect {
    
    self.m_tableView = [[IBTTableView alloc] initWithFrame:rect style:UITableViewStylePlain];
    _m_tableView.bHandleKeyboard = YES;
    _m_tableView.backgroundColor = [UIColor clearColor];
    _m_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_m_tableView registerClass:[IBTTableViewCell class] forCellReuseIdentifier:kCellID];
    _m_tableView.dataSource = self;
    _m_tableView.delegate = self;
    
    self.m_contentScrollView = _m_tableView;
    [self.view addSubview:_m_tableView];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    [self configureCell:cell forRowAtIndexPath:indexPath];
    
    if (self.m_currentIndexP && self.m_currentIndexP.row == indexPath.row) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    return cell;
}

- (void)configureCell:(UITableViewCell *)cell
    forRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSDictionary *dicOptionItem = self.m_question.details [ indexPath.row ];
    cell.textLabel.text = dicOptionItem[ @"content" ];
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:self.m_currentIndexP];
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    self.m_currentIndexP = indexPath;
    
    cell = [tableView cellForRowAtIndexPath:self.m_currentIndexP];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    NSDictionary *dicOptionItem = self.m_arrOptions[ indexPath.row ];
    
    ICRAnswerDetail *dE = [ICRAnswerDetail DBObject];
    dE.numberValue = [dicOptionItem[ @"index" ] unsignedIntegerValue];
    dE.uuid = [[ICRUserUtil sharedInstance] mobileID];
    
    self.m_answer.details = @[ [dE dictForCommit] ];
}

#pragma mark - Actions
- (void)onNextBtnAction:(__unused id)sender {
    
    if ([self.m_answer.details count] <= 0) {
        self.m_answer.bIsAnswered = NO;
        return;
    }
    
    self.m_answer.bIsAnswered = YES;
    
    ICRQuestionManager *mgr = [ICRQuestionManager sharedManager];
    UIViewController *qVC = [mgr questionViewControlAtIndex:self.m_uiIndex + 1];
    if (qVC) {
        [self PushViewController:qVC animated:YES];
    }
    else {
        [self openResultView];
    }
}

@end