DSHomeView.m
5.2 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
163
164
165
//
// DSHomeView.m
// DreamSleep
//
// Created by peter on 2022/5/11.
//
#import "DSHomeView.h"
#import "CourseMusicCell.h"
#import "GoodSleepSoundCell.h"
@interface DSHomeView () <UITableViewDelegate, UITableViewDataSource>
/// 通用数据
@property (nonatomic, strong) NSArray *courseMusicArr;
/// 舒眠课程数据
@property (nonatomic, strong) NSArray *courseListArr;
/// 助眠音乐数据
@property (nonatomic, strong) NSArray *helpListArr;
/// 类型数据
@property (nonatomic, strong) NSArray *typeArr;
@end
@implementation DSHomeView
- (instancetype)init {
if (self = [super initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kTopHeight(0) - kTabBarHeight) style:UITableViewStylePlain]) {
self.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
self.separatorStyle = UITableViewCellSeparatorStyleNone;
self.showsVerticalScrollIndicator = NO;
self.delegate = self;
self.dataSource = self;
self.tableHeaderView = self.headerView;
}
return self;
}
- (void)refreshBarnner:(NSArray *)bannerListData {
[self.headerView updateBannerWithListData:bannerListData];
}
- (void)updateCourseMusicCell:(CellType)type data:(NSArray *)dataArr {
if (dataArr && dataArr.count > 0) {
if (type == CellTypeCourse) {
self.courseListArr = dataArr;
} else if (type == CellTypeMusic) {
self.helpListArr = dataArr;
}
[self reloadData];
}
}
- (void)updateNoiseAllTypeData:(NSArray *)typeDataArr {
if (typeDataArr && typeDataArr.count > 0) {
self.typeArr = typeDataArr;
[self reloadData];
}
}
#pragma mark - UITableViewDelegate && UITableViewDataSource
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
NSString *cellID = [NSString stringWithFormat:@"cmg_%zd", indexPath.row];
if (indexPath.row == 2) {
GoodSleepSoundCell *goodCell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!goodCell) {
goodCell = [[GoodSleepSoundCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
[goodCell configBasicData:self.courseMusicArr[indexPath.row] cellIndex:indexPath.row];
[goodCell updateNoiseAllTypeData:self.typeArr];
return goodCell;
} else {
CourseMusicCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[CourseMusicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
[cell configBasicData:self.courseMusicArr[indexPath.row] cellIndex:indexPath.row];
[cell updateCourseMusicListWithData:(indexPath.row == 0 ? self.courseListArr : self.helpListArr) cellIndex:indexPath.row];
return cell;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.courseMusicArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height = 0;
if (indexPath.row == 0) {
height = 228;
} else if (indexPath.row == 1) {
height = 228;
} else if (indexPath.row == 2) {
height = 68 + 40 + 327 + 70;
}
return height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0.001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.001;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0.001)];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0.001)];
}
- (NSString *)cellIdentiferAtIndexPath:(NSIndexPath *)indexPath {
SafeSleepModel *item = self.courseMusicArr[indexPath.row];
if (item.type == CellTypeGood) {
return NSStringFromClass([GoodSleepSoundCell class]);
} else {
return NSStringFromClass([CourseMusicCell class]);
}
}
#pragma mark - lazy
- (HomeHeaderView *)headerView {
if (!_headerView) {
// banner高度
CGFloat bannerH = 2*(kScreenWidth - 48)/5.0;
// 哄睡按钮宽度
CGFloat width = kScreenWidth - 2*15 - 12;
CGFloat height = 15 + bannerH + 24 + (170*.3*width/104.0);
_headerView = [[HomeHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, height)];
_headerView.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
}
return _headerView;
}
- (NSArray *)courseMusicArr {
if (!_courseMusicArr) {
_courseMusicArr = [SafeSleepModel getDatas];
}
return _courseMusicArr;
}
- (NSArray *)courseListArr {
if (!_courseListArr) {
_courseListArr = [NSArray array];
}
return _courseListArr;
}
- (NSArray *)helpListArr {
if (!_helpListArr) {
_helpListArr = [NSArray array];
}
return _helpListArr;
}
- (NSArray *)typeArr {
if (!_typeArr) {
_typeArr = [NSArray array];
}
return _typeArr;
}
@end