CommunityView.m
7.0 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//
// CommunityView.m
// DreamSleep
//
// Created by peter on 2022/9/20.
//
#import "CommunityView.h"
@interface CommunityView () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) UITableView *listView;
@property (nonatomic, strong) NSArray *listArr;
@end
@implementation CommunityView
#pragma mark - 初始化
- (instancetype)initWithDelegate:(id<CommunityViewDelegate>)delegate {
if (self = [super init]) {
self.delegate = delegate;
self.listArr = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10"];
self.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
[self addSubview:self.listView];
UIButton *publishBtn = [UIButton new];
[publishBtn setImage:[UIImage imageNamed:@"ic_fabu"] forState:UIControlStateNormal];
[publishBtn addTarget:self action:@selector(publishAction) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:publishBtn];
[publishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self).offset(-15);
make.bottom.equalTo(self).offset(-45);
}];
}
return self;
}
- (void)layoutSubviews {
self.listView.frame = CGRectMake(15, 0, self.width - 30, self.height);
}
#pragma mark - public
- (void)updateCommunityMoments:(BOOL)loadMore {
if (loadMore) {
self.listArr = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"8", @"9", @"10"];
[self.listView.mj_footer endRefreshing];
} else {
[self.listView.mj_header endRefreshing];
}
[self.listView reloadData];
}
#pragma mark - Action
- (void)tapAction:(UITapGestureRecognizer *)tapGR {
if (self.delegate && [self.delegate respondsToSelector:@selector(tapCommunityHeaderModule:)]) {
[self.delegate tapCommunityHeaderModule:tapGR.view.tag];
}
}
- (void)publishAction {
if (self.delegate && [self.delegate respondsToSelector:@selector(publishLogicDeal)]) {
[self.delegate publishLogicDeal];
}
}
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.listArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 150;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return CGFLOAT_MIN;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return section == self.listArr.count - 1 ? 6.5 : 12;;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return [UIView new];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
[cell cornerRadius:24.0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, CornerViewDarkColor, DSWhite);
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectItem:)]) {
[self.delegate didSelectItem:indexPath];
}
}
#pragma mark - lazy
- (UITableView *)listView {
if (!_listView) {
_listView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
_listView.delegate = self;
_listView.dataSource = self;
_listView.backgroundColor = DSClearColor;
_listView.showsVerticalScrollIndicator = NO;
_listView.separatorStyle = UITableViewCellSeparatorStyleNone;
_listView.tableHeaderView = self.headView;
[_listView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
WS(weakSelf);
_listView.mj_header = [DSGifHeader headerWithRefreshingBlock:^{
if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(getDynamicListRequest:)]) {
[weakSelf.delegate getDynamicListRequest:NO];
}
}];
[_listView.mj_header beginRefreshing];
_listView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(getDynamicListRequest:)]) {
[weakSelf.delegate getDynamicListRequest:YES];
}
}];
}
return _listView;
}
- (UIView *)headView {
if (!_headView) {
CGFloat aroundMargin = 15;
CGFloat middleSpace = 12;
CGFloat articleScale = 146.0 / (kBaseW - 2*aroundMargin - middleSpace);
CGFloat totalW = kScreenWidth - 2*aroundMargin - middleSpace;
CGFloat articleW = articleScale * totalW;
CGFloat evaluationW = totalW * (1 - articleScale);
CGFloat articleH = 80 * articleW / 146;
_headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.listView.width, articleH + 2*aroundMargin)];
_headView.backgroundColor = DSClearColor;
UIImageView *articleIV = [self plateWithFrame:CGRectMake(0, 15, articleW, articleH) title:@"#睡眠文章" desc:@"精选海量好文" imgName:@"btn_shequ_shuimianwenzhang" tag:1];
[self plateWithFrame:CGRectMake(articleW + 12, articleIV.y, evaluationW, articleH) title:@"#人气测评" desc:@"玩转趣味测试" imgName:@"btn_shequ_ceping" tag:2];
}
return _headView;
}
- (UIImageView *)plateWithFrame:(CGRect)frame title:(NSString *)title desc:(NSString *)desc imgName:(NSString *)imgName tag:(NSInteger)tag {
UIImageView *imgView = [[UIImageView alloc] initWithFrame:frame];
[imgView dk_setAlphaPicker:DKAlphaPickerWithAlphas(1.0, .5, .5)];
imgView.image = [UIImage imageNamed:imgName];
imgView.tag = tag;
imgView.userInteractionEnabled = YES;
[self.headView addSubview:imgView];
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[imgView addGestureRecognizer:tapGR];
UILabel *titleLab = [UILabel labWithText:title textColor:DSWhite font:BoldFont(16)];
[imgView addSubview:titleLab];
UILabel *descLab = [UILabel labWithText:desc textColor:ColorFromHexA(0xFFFFFF, .5) font:SysFont(14)];
[imgView addSubview:descLab];
[titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.equalTo(imgView).offset(15);
make.height.equalTo(@22);
}];
[descLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(titleLab);
make.bottom.equalTo(imgView).offset(-15);
}];
return imgView;
}
@end