DiaryVC.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //
  2. #import "DiaryVC.h"
  3. #import "CommunityCell.h"
  4. #import "PostTopicVC.h"
  5. #import "TopicVC.h"
  6. #import "STButton.h"
  7. @interface DiaryVC ()<UITableViewDataSource,UITableViewDelegate,CommunityCellDelegate>
  8. {
  9. NSMutableArray* models;
  10. UITableView* myTableView;
  11. /**每次下载后。就判断.
  12. 如果cnt为0就显示。否则隐藏
  13. */
  14. UIView* placeHolder;
  15. NSInteger currentPage;
  16. }
  17. @end
  18. @implementation DiaryVC
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. currentPage = 1;
  22. models = [NSMutableArray array];
  23. [self myInit];
  24. }
  25. - (void)didReceiveMemoryWarning {
  26. [super didReceiveMemoryWarning];
  27. }
  28. -(void)viewDidAppear:(BOOL)animated
  29. {
  30. [super viewDidAppear:animated];
  31. currentPage = 1;
  32. [self getTopicByUser];
  33. }
  34. #pragma mark -
  35. -(void)myInit
  36. {
  37. [self setTitle:@"我的学车日记"];
  38. [self configNavigationBar];
  39. //1.0.4 修复不能显示别人学车日记的bug
  40. self.view.backgroundColor = backGroundColor;
  41. if(![_userID isEqualToString:defUser.userDict[@"id"]])
  42. {
  43. [self setTitle:@"学车日记"];
  44. }
  45. //25+10
  46. UITableView* tv = [[UITableView alloc] initWithFrame:CGRectMake(25, 0, kSize.width - 35, kSize.height - kNavOffSet - 60 - kSafeAreaBottomHeight) style:UITableViewStyleGrouped];
  47. [tv setBackgroundColor:backGroundColor];
  48. [self.view addSubview:tv];
  49. [tv setDelegate:self];
  50. [tv setDataSource:self];
  51. tv.estimatedSectionFooterHeight = 0;
  52. myTableView = tv;
  53. [tv setTableFooterView:[UIView new]];
  54. if ([tv respondsToSelector:@selector(setSeparatorInset:)])
  55. {
  56. [tv setSeparatorInset:UIEdgeInsetsZero];
  57. }
  58. if ([tv respondsToSelector:@selector(setLayoutMargins:)])
  59. {
  60. [tv setLayoutMargins:UIEdgeInsetsZero];
  61. }
  62. NSInteger tag = 0;
  63. CGFloat h,x,w,y;
  64. //placeHolder
  65. UIView* vi = [[UIView alloc] initWithFrame:kFrame];
  66. [self.view addSubview:vi];
  67. [vi setBackgroundColor:backGroundColor];
  68. vi.hidden = YES;
  69. placeHolder = vi;
  70. w = h = kSize.width/3.0;
  71. x = y = w;
  72. UIImageView* iv = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, w, h)];
  73. [iv setImage:[UIImage imageNamed:@"community4.png"]];
  74. [iv setContentMode:UIViewContentModeScaleAspectFit];
  75. [placeHolder addSubview:iv];
  76. y += h + 20;
  77. x = 0;
  78. w = kSize.width;
  79. h = 40;
  80. UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
  81. [label setText:@"学车记忆宝贵,为何不留下点什么~"];
  82. [label setTextAlignment:NSTextAlignmentCenter];
  83. [label setTextColor:contentTextColor];
  84. [placeHolder addSubview:label];
  85. //tabbar
  86. h = 60;
  87. UIView* bar = [[UIView alloc] initWithFrame:CGRectMake(0, kSize.height - kNavOffSet -kSafeAreaBottomHeight-h, kSize.width, h )];
  88. [bar setBackgroundColor:backGroundColor];
  89. [bar addSelfViewWithRect:CGRectMake(0, 0, kSize.width, 1) Color:KlineColor];
  90. [self.view addSubview:bar];
  91. w = 100;
  92. x = (kSize.width - w)/2.0;
  93. STButton* stBtn = [[STButton alloc] initWithFrame:CGRectMake(x, 0, w, h)];
  94. [stBtn setImage:[UIImage imageNamed:@"community2.png"] withTitle:@"发布话题" Font:SmallFont forState:UIControlStateNormal];
  95. [stBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  96. [stBtn setStyle:1];
  97. [stBtn setTitleColor:contentTextColor forState:UIControlStateNormal];
  98. [bar addSubview:stBtn];
  99. [stBtn setTag:tag++];
  100. }
  101. -(void)btnClick:(UIButton*)sender
  102. {
  103. PostTopicVC* vc = [[PostTopicVC alloc] init];
  104. vc.type = @"1";
  105. [self navPushHideTabbarToVC:vc];
  106. }
  107. #pragma mark -
  108. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  109. {
  110. return 30;
  111. }
  112. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  113. {
  114. return 1;
  115. }
  116. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  117. {
  118. NSDictionary* obj = models[section];
  119. UILabel* v = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kSize.width, 30)];
  120. [v setText:obj[@"TI_CRDATE"]];
  121. [v setTextColor:defGreen];
  122. [v setFont:[UIFont scaleSize:12]];
  123. // setRandomColor(v);
  124. return v;
  125. }
  126. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  127. {
  128. return communityCellHeight(models[indexPath.section] , 2);
  129. }
  130. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  131. {
  132. return 1;
  133. //return models.count;
  134. }
  135. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  136. {
  137. return models.count;
  138. }
  139. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  140. {
  141. CommunityCell* cell = [CommunityCell cellForTableView:tableView Style:2];
  142. //用于通知VC跳转到评论
  143. [cell setBackgroundColor:[UIColor whiteColor]];
  144. cell.layer.cornerRadius = 5;
  145. cell.layer.masksToBounds = YES;
  146. [cell setDelegate:self];
  147. [cell setModel:models[indexPath.section]];
  148. return cell;
  149. }
  150. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  151. {
  152. NSDictionary* obj = models[indexPath.section];
  153. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  154. TopicVC* vc = [[TopicVC alloc] init];
  155. vc.topicId = obj[@"ID"];
  156. [self navPushHideTabbarToVC:vc];
  157. }
  158. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  159. {
  160. if ([cell respondsToSelector:@selector(setSeparatorInset:)])
  161. {
  162. [cell setSeparatorInset:UIEdgeInsetsZero];
  163. }
  164. if ([cell respondsToSelector:@selector(setLayoutMargins:)])
  165. {
  166. [cell setLayoutMargins:UIEdgeInsetsZero];
  167. }
  168. }
  169. -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  170. {
  171. CGPoint off = scrollView.contentOffset;
  172. if (scrollView.contentSize.height - off.y - scrollView.frame.size.height < 1) {
  173. [scrollView setContentOffset:CGPointMake(off.x, off.y - 10) animated:YES];
  174. [self getTopicByUser];
  175. }
  176. }
  177. #pragma mark - 数据请求
  178. /**对某一话题进行评论
  179. */
  180. -(void)gotoComment:(NSString *)comId
  181. {
  182. if (!myDelegate.isLogin) {
  183. return;
  184. }
  185. TopicVC* vc = [[TopicVC alloc] init];
  186. [vc setTopicId:comId];
  187. [vc setTextViewActive:YES];
  188. [self navPushHideTabbarToVC:vc];
  189. }
  190. /**下载的json里面没有头像地址。
  191. */
  192. -(void)getTopicByUser
  193. {
  194. if (![Util connectedToNetWork]) {
  195. showMsgUnconnect();
  196. return;
  197. }
  198. if(!_userID){
  199. _userID = defUser.userDict[@"id"];
  200. }
  201. NSMutableArray *arr=[NSMutableArray array];
  202. [arr addPro:@"user" Value:_userID];
  203. [arr addPro:@"isPage" Value:@"1"];
  204. [arr addPro:@"pageSize" Value:@"10"];
  205. [arr addPro:@"currentPage" Value:[NSString stringWithFormat:@"%d",(int)currentPage]];
  206. NSString* method = @"getTopicByUser";
  207. [MBProgressHUD showLoadToView:self.view];
  208. [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary *dict) {
  209. [MBProgressHUD hideHUDForView:self.view];
  210. if (models.count == 0) {
  211. placeHolder.hidden = NO;
  212. }else{
  213. placeHolder.hidden = YES;
  214. }
  215. if (!dict) {
  216. ShowMsgFailed();
  217. return;
  218. }
  219. if ( [dict[@"code"] isEqualToString:@"1"]) {
  220. ShowMsg(dict[@"body"]);
  221. return;
  222. }
  223. NSArray* json = dict[@"body"];
  224. if (json.count < 1) {
  225. ShowMsg(@"已加载全部");
  226. return;
  227. }
  228. if (currentPage == 1) {
  229. [models removeAllObjects];
  230. [models addObjectsFromArray:json];
  231. placeHolder.hidden = YES;
  232. [myTableView reloadData];
  233. }else{
  234. [models addObjectsFromArray:json];
  235. placeHolder.hidden = YES;
  236. [myTableView reloadData];
  237. }
  238. currentPage ++;
  239. }];
  240. }
  241. @end