LearninglistViewController.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // LearninglistViewController.m
  3. // ChinaTheoryNetwork
  4. //
  5. // Created by 张毅成 on 2019/1/28.
  6. // Copyright © 2019 张毅成. All rights reserved.
  7. //
  8. #import "LearninglistViewController.h"
  9. #import "LearninglistTableViewCell.h"
  10. #import "BlacklistModel.h"
  11. #define cacheKey @"LearninglistCacheKey"
  12. @interface LearninglistViewController ()<UITableViewDelegate, UITableViewDataSource>
  13. @property (weak, nonatomic) IBOutlet UITableView *tableView;
  14. @property (strong, nonatomic) NSMutableArray *arrayData;
  15. @end
  16. @implementation LearninglistViewController
  17. - (NSMutableArray *)arrayData {
  18. if (!_arrayData) {
  19. _arrayData = @[].mutableCopy;
  20. }
  21. return _arrayData;
  22. }
  23. - (void)Network {
  24. NSString *URL = @"my/studyRank";
  25. NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
  26. [[HttpManager sharedHttpManager] GET:URL parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  27. NSLog(@"%@",responseObject);
  28. if ([responseObject[@"code"] integerValue] == 0) {
  29. NSArray *array = [NSArray modelArrayWithClass:[BlacklistModel class] json:responseObject[@"data"][@"memberUserInfoList"]].mutableCopy;
  30. [array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  31. BlacklistModel *modelNew = obj;
  32. [self.arrayData enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  33. BlacklistModel *modelOld = obj;
  34. if ([modelNew.UserId isEqualToString:modelOld.UserId]) {
  35. modelNew.isLike = modelOld.isLike;
  36. }
  37. }];
  38. }];
  39. [self.arrayData removeAllObjects];
  40. [self.arrayData addObjectsFromArray:array];
  41. [self.tableView reloadData];
  42. YYCache *cache = [YYCache cacheWithName:@"ZYCCache"];
  43. [cache setObject:self.arrayData forKey:cacheKey];
  44. }
  45. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  46. }];
  47. }
  48. - (void)viewDidLoad {
  49. [super viewDidLoad];
  50. self.title = @"学习榜单";
  51. [self createTableView];
  52. [self getCache];
  53. [self Network];
  54. }
  55. - (void)viewWillDisappear:(BOOL)animated {
  56. [super viewWillDisappear:animated];
  57. YYCache *cache = [YYCache cacheWithName:@"ZYCCache"];
  58. [cache setObject:self.arrayData forKey:cacheKey];
  59. }
  60. - (void)createTableView {
  61. self.tableView.delegate = self;
  62. self.tableView.dataSource = self;
  63. self.tableView.tableFooterView = [UIView new];
  64. self.tableView.rowHeight = UITableViewAutomaticDimension;
  65. self.tableView.estimatedRowHeight = 60;
  66. // self.tableView.separatorStyle = 0;
  67. }
  68. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  69. return 1;
  70. }
  71. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  72. return self.arrayData.count;
  73. }
  74. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  75. LearninglistTableViewCell *cell = [LearninglistTableViewCell cellWithTableView:tableView];
  76. BlacklistModel *model = self.arrayData[indexPath.row];
  77. cell.labelTitile.text = model.BlackUserName;
  78. cell.labelTime.text = model.LastModifiedDate;
  79. [cell.imageViewIcon sd_setImageWithURL:[NSURL URLWithString:model.AvatarUrl] placeholderImage:kUserDefaultHeadImage];
  80. cell.labelNumber.text = @(indexPath.row + 1).stringValue;
  81. cell.buttonLike.selected = model.isLike;
  82. cell.blockDidTouchButtonLike = ^{
  83. model.isLike = !model.isLike;
  84. [tableView reloadRowAtIndexPath:indexPath withRowAnimation:(UITableViewRowAnimationNone)];
  85. };
  86. if (indexPath.row < 3) {
  87. [cell.labelNumber setTextColor:[UIColor redColor]];
  88. }else{
  89. [cell.labelNumber setTextColor:[UIColor hexStringToColor:@"666666"]];
  90. }
  91. return cell;
  92. }
  93. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  94. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  95. [cell setSeparatorInset:UIEdgeInsetsZero];
  96. }
  97. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  98. [cell setLayoutMargins:UIEdgeInsetsZero];
  99. }
  100. if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  101. [cell setPreservesSuperviewLayoutMargins:NO];
  102. }
  103. }
  104. - (void)getCache {
  105. [self.arrayData removeAllObjects];
  106. YYCache *cache = [YYCache cacheWithName:@"ZYCCache"];
  107. BOOL isContains = [cache containsObjectForKey:cacheKey];
  108. NSLog(@"%d",isContains);
  109. id value = [cache objectForKey:cacheKey];
  110. NSLog(@"%@",value);
  111. if (value) {
  112. self.arrayData = value;
  113. [self.tableView reloadData];
  114. }
  115. }
  116. @end