JTOptionsViewController.m 4.19 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 140 141 142 143 144 145 146 147 148 149 150 151
//
//  JTOptionsViewController.m
//  JobTalk
//
//  Created by Xummer on 14-5-22.
//  Copyright (c) 2014年 BST. All rights reserved.
//

static NSString *optinsCellIdentifier = @"JTOptionsCell";

#import "JTOptionsViewController.h"
#import "IBTTableView.h"
#import "IBTTableViewCell.h"
#import "JTOptionCell.h"

@interface JTOptionsViewController ()
<
    UITableViewDataSource,
    UITableViewDelegate
>

@property (strong, nonatomic) IBTTableView *tableView;
@property (strong, nonatomic) JTOptionsEntity *optionsData;

@end

@implementation JTOptionsViewController

#pragma mark - Life Cycle
- (id)initWithOptions:(JTOptionsEntity *)options {
    self = [super init];
    if (!self) {
        return nil;
    }
    
    self.optionsData = options;
    
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupSubviews];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
//    [_tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:_optionsData.selectedIndex inSection:0]
//                            animated:YES
//                      scrollPosition:UITableViewScrollPositionMiddle];
}

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


#pragma mark - Private Method
- (void)setupSubviews {
    // setup tableview
    
    CGRect frame = self.view.bounds;
    self.tableView =
    [[IBTTableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
    _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [_tableView registerClass:[JTOptionCell class]
       forCellReuseIdentifier:optinsCellIdentifier];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    [self.view addSubview:_tableView];
    
}

- (id)optionsDataAtIndexPath:(NSIndexPath *)indexPath {
    return _optionsData.optionsArray[ indexPath.row ];
}

- (NSString *)optionDisplayDataAtIndexPath:(NSIndexPath *)indexPath {
    return _optionsData.m_arrDisplayStr[ indexPath.row ];
}

#pragma mark - UITableViewDataSource

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

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    return [_optionsData.optionsArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:optinsCellIdentifier forIndexPath:indexPath];
    
    [self configureCell:cell forRowAtIndexPath:indexPath];
    
    return cell;
}

- (void)configureCell:(UITableViewCell *)cell
    forRowAtIndexPath:(NSIndexPath *)indexPath
{
//    cell.selected = (indexPath.row == _optionsData.selectedIndex);
    cell.textLabel.text = [IBTCommon localizableString:[self optionDisplayDataAtIndexPath:indexPath]];
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.optionsData.selectedIndex = indexPath.row;
    
    if (_baseCellIndexPath) {
        if ([_optionsDelegate respondsToSelector:@selector(optionsViewController:didChangedSelectedOptions:atBaseIndexPath:)]) {
            [_optionsDelegate optionsViewController:self didChangedSelectedOptions:_optionsData atBaseIndexPath:_baseCellIndexPath];
        }
    }
    else {
        if ([_optionsDelegate respondsToSelector:@selector(optionsViewController:didChangedSelectedOptions:)]) {
            [_optionsDelegate optionsViewController:self didChangedSelectedOptions:_optionsData];
        }
    }
    
    self.tableView.userInteractionEnabled = NO;
    
    if ((![self isPushedIn]) && [self isPresentedIn]) {
        double delayInSeconds = .2f;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [self dismissViewControllerAnimated:YES completion:NULL];
        });
    }
    else {
        [self performSelector:@selector(PopViewControllerAnimated:)
                   withObject:@( YES )
                   afterDelay:.2f];
    }
}

@end