UserInfoTableView.m
7.5 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
185
186
//
// UserInfoTableView.m
// DreamSleep
//
// Created by peter on 2022/4/19.
//
#import "UserInfoTableView.h"
#import "NickNameController.h"
#import "InfoModifyAlertView.h"
#import "WSDatePickerView.h"
/// 自定义用户信息显示数据model
@interface UserShowModel : NSObject
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *detail;
// 获取用户显示数据
+ (NSArray *)getUserShowDatas;
@end
@implementation UserShowModel
+ (NSArray *)getUserShowDatas {
NSArray *titles = @[@"昵称", @"性别", @"生日"];
UserModel *userModel = [LoginUtils getUserLoginData];
NSString *nick_name = userModel && userModel.nick_name ? userModel.nick_name : @"";
NSString *gender = userModel ? (userModel.gender ? @"男" : @"女") : @"";
NSString *birthday = userModel && userModel.birthday ? userModel.birthday : @"";
NSArray *details = @[nick_name, gender, birthday];
NSMutableArray *tmpArr = [NSMutableArray array];
for (int i = 0; i < titles.count; i++) {
UserShowModel *m = [UserShowModel new];
m.title = titles[i];
m.detail = details[i];
[tmpArr addObject:m];
}
return [tmpArr copy];
}
@end
/// 设置自定义Cell
@interface UserCell : UITableViewCell
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *detailLab;
@property (nonatomic, strong) UIImageView *rightIcon;
- (void)updateUIWithModel:(UserShowModel *)model;
@end
@implementation UserCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, CornerViewDarkColor, DSWhite);
self.rightIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"moreIcon"]];
[self.contentView addSubview:self.rightIcon];
self.titleLab = [UILabel dkLabWithFont:SysFont(15.0)];
[self.contentView addSubview:self.titleLab];
self.detailLab = [UILabel labWithFont:SysFont(14.0)];
self.detailLab.dk_textColorPicker = DKColorPickerWithColors(SubTitleColor, ColorFromHex(0x8C8F9D), DSWhite);
[self.contentView addSubview:self.detailLab];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame];
self.selectedBackgroundView.backgroundColor = DSClearColor;
[super setSelected:selected animated:animated];
}
- (void)updateUIWithModel:(UserShowModel *)model {
self.titleLab.text = model.title;
self.detailLab.text = model.detail;
[self.titleLab sizeToFit];
[self.detailLab sizeToFit];
[self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(15);
make.centerY.equalTo(self.contentView);
}];
[self.detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.rightIcon.mas_left).offset(-8);
make.centerY.equalTo(self.contentView);
}];
[self.rightIcon mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.contentView);
make.right.equalTo(self.contentView).offset(-8);
}];
}
@end
@interface UserInfoTableView () <UITableViewDelegate, InfoModifyAlertViewDelegate>
@property (nonatomic, strong) DSDataSource *infoDataSource;
@end
@implementation UserInfoTableView
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
if (self = [super initWithFrame:frame style:style]) {
[self cornerRadius:10];
self.scrollEnabled = NO;
self.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, CornerViewDarkColor, DSWhite);
self.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.infoDataSource addDataArray:[UserShowModel getUserShowDatas]];
}
return self;
}
- (DSDataSource *)infoDataSource {
if (!_infoDataSource) {
CellConfigureBlock cellBlock = ^(UserCell * cell, UserShowModel * model, NSIndexPath * indexPath) {
[cell updateUIWithModel:model];
};
NSString * const userCell = @"UserCell";
_infoDataSource = [[DSDataSource alloc] initWithIdentifier:userCell datas:@[] isSection:NO configureBlock:cellBlock];
self.dataSource = _infoDataSource;
self.delegate = self;
[self registerClass:[UserCell class] forCellReuseIdentifier:userCell];
}
return _infoDataSource;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) { // 修改昵称
WS(weakSelf);
NickNameController *nickVC = [[NickNameController alloc] initWithBlock:^{
[weakSelf.infoDataSource addDataArray:[UserShowModel getUserShowDatas]];
[weakSelf reloadData];
}];
[self.ds_viewController.navigationController pushViewController:nickVC animated:YES];
} else if (indexPath.row == 1) { // 修改性别
InfoModifyAlertView *genderView = [[InfoModifyAlertView alloc] initWithDelegate:self type:AlertTypeGender];
[genderView showInfoModifyAlertView];
} else if (indexPath.row == 2) { // 修改生日
WS(weakSelf);
NSDate *birthDate = [NSDate date:[LoginUtils getBirthday] WithFormat:@"yyyy-MM-dd"];
NSDate *defaultDate = [NSDate date:@"1975-07-15" WithFormat:@"yyyy-MM-dd"];
NSDate *scrollDate = birthDate ? birthDate : defaultDate;
WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonthDay scrollToDate:scrollDate CompleteBlock:^(NSDate *selectDate) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *selectDateStr = [dateFormatter stringFromDate:selectDate];
NSDictionary *data = @{@"birthday" : selectDateStr};
[DSProgressHUD showProgressHUDWithInfo:@""];
[UserRequestModel updateUserInfoWithData:data completion:^(UserRequestModel * _Nonnull requestModel) {
[DSProgressHUD dissmissProgressHUD];
if (requestModel.resCode == DSResCodeSuccess) {
[DSProgressHUD showToast:@"修改成功"];
[LoginUtils updateUserInfo:data];
[weakSelf.infoDataSource addDataArray:[UserShowModel getUserShowDatas]];
[weakSelf reloadData];
}
}];
}];
[datepicker setMinLimitDate:[NSDate date:@"1930-01-01" WithFormat:@"yyyy-MM-dd"]];
[datepicker setMaxLimitDate:[NSDate date]];
[datepicker show];
}
}
#pragma mark - InfoModifyAlertViewDelegate && 修改性别请求
- (void)didSelectWithType:(AlertType)type index:(int)index {
WS(weakSelf);
NSDictionary *data = @{@"gender" : @(index == 1 ? 1 : 0)};
[DSProgressHUD showProgressHUDWithInfo:@""];
[UserRequestModel updateUserInfoWithData:data completion:^(UserRequestModel * _Nonnull requestModel) {
[DSProgressHUD dissmissProgressHUD];
if (requestModel.resCode == DSResCodeSuccess) {
[DSProgressHUD showToast:@"修改成功"];
[LoginUtils updateUserInfo:data];
[weakSelf.infoDataSource addDataArray:[UserShowModel getUserShowDatas]];
[weakSelf reloadData];
}
}];
}
@end