YHL_RedPacketAndHotSaleViewController.m 5.41 KB
//
//  YHL_RedPacketAndHotSaleViewController.m
//  YohoLive
//
//  Created by 盖剑秋 on 2016/11/8.
//  Copyright © 2016年 YOHO!. All rights reserved.
//

#import "YHL_RedPacketAndHotSaleViewController.h"
#import "YHL_HotSaleTableViewCell.h"
#import "YHL_RedPacketTableViewCell.h"

@interface YHL_RedPacketAndHotSaleViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, assign) LiveDataType currentType;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, copy) void (^completionBlock)(NSInteger selectedIndex);

@end

@implementation YHL_RedPacketAndHotSaleViewController
{
    UIButton *commitButton;
    NSInteger selectedIndex;
}
+ (void)showInViewController:(UIViewController *)aController withDataType:(LiveDataType)dataType andDataArray:(NSArray *)dataAry completionBlock:(void (^)(NSInteger))block {
    
    YHL_RedPacketAndHotSaleViewController *ctl = [YHL_RedPacketAndHotSaleViewController new];
    ctl.currentType = dataType;
    ctl.dataArray = dataAry;
    ctl.completionBlock = [block copy];
    
    [aController addChildViewController:ctl];
    [aController.view addSubview:ctl.view];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    selectedIndex = -1;
    self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
    self.view.alpha = 0;
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(-115);
        make.height.mas_equalTo(380);
    }];
    
    commitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [commitButton setBackgroundColor:[UIColor lightGrayColor]];
    [commitButton setTitle:@"发送" forState:UIControlStateNormal];
    [commitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    commitButton.titleLabel.font = [UIFont systemFontOfSize:17];
    [commitButton addTarget:self action:@selector(commitButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:commitButton];
    [commitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(-64);
        make.height.mas_equalTo(40);
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self tapToDismiss];
}

- (void)tapToDismiss {
    [UIView animateWithDuration:0.3 animations:^{
        self.view.alpha = 0;
    } completion:^(BOOL finished) {
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
    }];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [UIView animateWithDuration:0.3 animations:^{
       
        self.view.alpha = 1;
    }];
}

- (void)commitButtonPressed {
    if (selectedIndex<0) {
        return;
    }
    if (_completionBlock) {
        _completionBlock(selectedIndex);
    }
    [self tapToDismiss];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 122;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    selectedIndex = indexPath.row;
    [commitButton setBackgroundColor:[UIColor blackColor]];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (_currentType) {
        case LiveDataTypeHotSale:
        {
            YHL_HotSaleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHL_HotSaleTableViewCell"];
            if (!cell) {
                cell = [[YHL_HotSaleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YHL_HotSaleTableViewCell"];
            }
            [cell bindModel:_dataArray[indexPath.row]];
            return cell;
        }
            break;
        case LiveDataTypeRedPacket:
        {
            YHL_RedPacketTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHL_RedPacketTableViewCell"];
            if (!cell) {
                cell = [[YHL_RedPacketTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YHL_RedPacketTableViewCell"];
            }
            NSDictionary *dic = _dataArray[indexPath.row];
            NSString *name = dic[@"type_name"];
            if (name.length) {
                [cell bindRedPacketName:name];
            }
            return cell;
        }
            break;
        default:
            break;
    }
    
    return [UITableViewCell new];
}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end