ModelHonorViewController.m 6.2 KB

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