YHL_RedPacketAndHotSaleViewController.m
5.41 KB
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
152
153
154
155
156
157
158
159
160
161
162
//
// 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