ModelHonorViewController.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // ModelHonorViewController.m
  3. // model
  4. //
  5. // Created by liufei on 2018/7/25.
  6. // Copyright © 2018年 Mine. All rights reserved.
  7. //
  8. #import "ModelHonorViewController.h"
  9. #import "HonorTableViewCell.h"
  10. @interface ModelHonorViewController () <UITableViewDelegate, UITableViewDataSource, UITextViewDelegate>
  11. @property(nonatomic, strong) UITableView *listView;
  12. @property(nonatomic, strong) NSMutableArray *dataSource;
  13. @end
  14. @implementation ModelHonorViewController
  15. - (NSMutableArray *)dataSource {
  16. if (!_dataSource) {
  17. _dataSource = [NSMutableArray array];
  18. }
  19. return _dataSource;
  20. }
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. self.view.backgroundColor = RGBValueColor(0xf7f7f7, 1.0);
  24. self.title = @"我的荣誉";
  25. self.navigationController.navigationBar.tintColor = RGBValueColor(0x333333, 1);
  26. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(addHonor)];
  27. [self getHonorData];
  28. }
  29. - (void)getHonorData {
  30. [YanCNetWorkManager requestGETWithURLStr:Url_getModelHonor(PublicUrl)
  31. parameters:@{@"ModelPK": [ModelUser user].modelpk}
  32. finish:^(id dataDic) {
  33. if ([dataDic[@"msg"] isEqualToString:@"success"]) {
  34. NSArray *array = dataDic[@"data"];
  35. for (int i = 0; i < array.count; i++) {
  36. NSDictionary *dic = array[i];
  37. [self.dataSource addObject:dic[@"honor"]];
  38. }
  39. [self configUI];
  40. }
  41. }
  42. enError:^(NSError *error) {
  43. }];
  44. }
  45. - (void)configUI {
  46. self.listView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
  47. self.listView.delegate = self;
  48. self.listView.dataSource = self;
  49. self.listView.backgroundColor = RGBValueColor(0xf7f7f7, 1.0);
  50. self.listView.rowHeight = 143.f;
  51. self.listView.tableFooterView = [self footView];
  52. [self.listView registerNib:[UINib nibWithNibName:@"HonorTableViewCell" bundle:nil] forCellReuseIdentifier:NSStringFromClass([HonorTableViewCell class])];
  53. [self.view addSubview:self.listView];
  54. }
  55. - (void)addHonor {
  56. [self.dataSource addObject:@""];
  57. [self.listView reloadData];
  58. }
  59. - (void)handleSave {
  60. NSMutableArray *arr = [[NSMutableArray alloc] init];
  61. for (NSString *str in self.dataSource) {
  62. if (str.length > 0) {
  63. NSDictionary *dicc = @{@"honor": str};
  64. [arr addObject:dicc];
  65. }
  66. }
  67. NSString *jsonStr = [arr mj_JSONString];
  68. NSDictionary *dic = @{@"ModelPK": [ModelUser user].modelpk, @"Honor": jsonStr};
  69. [YanCNetWorkManager requestPostWithURLStr:Url_addModelHonor(PublicUrl) parameters:dic finish:^(id dataDic) {
  70. if ([[dataDic objectForKey:@"msg"] isEqualToString:@"success"]) {
  71. [MBProgressHUD showSuccess:@"保存成功"];
  72. [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(backClick) userInfo:nil repeats:NO];
  73. } else {
  74. [MBProgressHUD showSuccess:@"保存失败"];
  75. }
  76. } enError:^(NSError *error) {
  77. }];
  78. }
  79. - (void)backClick {
  80. [self.navigationController popViewControllerAnimated:YES];
  81. }
  82. - (UIView *)footView {
  83. UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 120)];
  84. UIButton *saveBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, 40, ScreenWidth - 40, 48)];
  85. saveBtn.layer.cornerRadius = 24.f;
  86. saveBtn.layer.masksToBounds = YES;
  87. [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
  88. [saveBtn addTarget:self action:@selector(handleSave) forControlEvents:UIControlEventTouchUpInside];
  89. [saveBtn setBackgroundImage:[UIImage imageNamed:@"tixian-1"] forState:UIControlStateNormal];
  90. [footView addSubview:saveBtn];
  91. return footView;
  92. }
  93. #pragma mark -
  94. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  95. return self.dataSource.count;
  96. }
  97. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  98. return 1;
  99. }
  100. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  101. static NSString *cellid = @"HonorTableViewCell";
  102. HonorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
  103. if (!cell) {
  104. cell = [[HonorTableViewCell alloc] init];
  105. }
  106. // HonorTableViewCell *cell = [[HonorTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HonorTableViewCell"];
  107. if (self.dataSource.count > 0) {
  108. cell.honorTextView.text = self.dataSource[indexPath.section];
  109. }
  110. cell.numberLabel.text = [NSString stringWithFormat:@"%zd", cell.honorTextView.text.length];
  111. cell.honorTextView.tag = indexPath.section;
  112. cell.honorTextView.delegate = self;
  113. return cell;
  114. }
  115. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  116. UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
  117. headView.backgroundColor = RGBValueColor(0xf7f7f7, 1.0);
  118. if (section == 0) {
  119. UILabel *headLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, ScreenWidth - 40, 40)];
  120. headLabel.font = [UIFont systemFontOfSize:14];
  121. headLabel.textColor = RGBValueColor(0x666666, 1);
  122. headLabel.text = @"我的荣誉";
  123. [headView addSubview:headLabel];
  124. }
  125. return headView;
  126. }
  127. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  128. return section == 0 ? 40.f : 10.f;
  129. }
  130. - (void)textViewDidEndEditing:(UITextView *)textView {
  131. [self.listView reloadData];
  132. }
  133. - (void)textViewDidChange:(UITextView *)textView {
  134. [self.dataSource replaceObjectAtIndex:textView.tag withObject:textView.text];
  135. }
  136. - (void)didReceiveMemoryWarning {
  137. [super didReceiveMemoryWarning];
  138. // Dispose of any resources that can be recreated.
  139. }
  140. /*
  141. #pragma mark - Navigation
  142. // In a storyboard-based application, you will often want to do a little preparation before navigation
  143. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  144. // Get the new view controller using [segue destinationViewController].
  145. // Pass the selected object to the new view controller.
  146. }
  147. */
  148. @end