#import "TopicesVC.h" #import "CommunityCell.h" #import "TopicVC.h" #import "LoginViewController.h" #import "PostTopicVC.h" #import "EXButton.h" #import typedef NS_ENUM(NSInteger, MyGetDataType) { //正常请求数据 MyGetDataTypeNomal=0, //下拉刷新请求数据 MyGetDataTypeHeaderRefresh, //上拉加载更多请求数据 MyGetDataTypeFooterRefresh }; @interface TopicesVC () { /**多个topic */ NSMutableArray* models; UITableView* myTableView; UILabel* lblTopicCnt,* lblStuCnt; UIView* moodV; UIButton* moodBg; //记录页书 NSInteger _currentPageNum; //加载数据的类型 MyGetDataType _getDataType; //正在加载的状态 BOOL _IS_LOADING; } @end @implementation TopicesVC - (void)viewDidLoad { [super viewDidLoad]; [self myInit]; [self setRefreshAction]; [self getModelsAsyc]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } #pragma mark - /**注意这里是导航栏带来的Y值偏移 */ -(void)myInit { [self configNavigationBar]; self.navigationController.navigationBar.translucent = NO; [self.view setBackgroundColor:[UIColor whiteColor]]; _currentPageNum = 0; _getDataType = MyGetDataTypeNomal; _IS_LOADING = NO; models = [NSMutableArray array]; CGFloat x,y,w,h,bd; //最好是。它们的tag值不要变。 NSArray* arr = @[@"最新话题",@"吐槽",@"晒图",@"找驾校",@"科目一", @"科目二",@"科目三",@"科目四",@"下证",@"许愿", @"科目一学员",@"科目一考试",@"科目二学员",@"科目二考试",@"科目三学员", @"科目三考试",@"科目四学员",@"下证学员",@"报名学员",@"unknown",@"unknown",@"unknown"]; if (1 == _type.intValue || 2 == _type.intValue) { if ([_groupId isEqualToString:@"-1"]) { if (_jxjc.length == 0) { if (defUser.userSchDict) { NSString* str = [defUser.userSchDict objectForKey:@"shortName"]; [self setTitle:str]; } } else { [self setTitle:_jxjc]; } }else{ [self setTitle:arr[_groupId.intValue]]; } } if( 2 == [_type intValue]){ [self setTitle:@"学车记录"]; } if( 3 == [_type intValue]){ [self setTitle:@"学车直播"]; } if (4 == _type.intValue) { [self setTitle:@"学车问答"]; } y = 0; x = 0; w = kSize.width; h = kSize.width*201/721; UIImageView* imgV = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, w, h)]; [imgV setImage:[UIImage imageNamed:@"subject_main_banner.jpg"]]; [imgV setContentMode:UIViewContentModeScaleAspectFill]; // h += 40; UIView *header = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)]; [header addSubview:imgV]; y = h-40; h = 40; w = kSize.width/2.0; x = 0; UILabel* label; label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)]; [label setFont:[UIFont scaleSize:NormalFont]]; [label setBackgroundColor:[UIColor colorWithWhite:.2 alpha:.6]]; [label setTextAlignment:NSTextAlignmentCenter]; [label setTextColor:[UIColor whiteColor]]; [label setText:@"话题"]; // [header addSubview:label]; lblTopicCnt = label; x+=w; label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)]; [label setBackgroundColor:[UIColor colorWithWhite:.2 alpha:.6]]; [label setFont:[UIFont scaleSize:NormalFont]]; [label setTextAlignment:NSTextAlignmentCenter]; [label setTextColor:[UIColor whiteColor]]; [label setText:@"驾友"]; // [header addSubview:label]; lblStuCnt = label; UITableView *tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kSize.width, kSize.height - 60 - kNavOffSet - kSafeAreaBottomHeight) style:UITableViewStyleGrouped]; [self.view addSubview:tv]; //当把table的style设为group之后。table的背景色莫名变灰 [tv setBackgroundColor:[UIColor whiteColor]]; myTableView = tv; [myTableView setDelegate:self]; [myTableView setDataSource:self]; [myTableView setTableHeaderView:header]; [myTableView setTableFooterView:[UIView new]]; if ([tv respondsToSelector:@selector(setSeparatorInset:)]) { [tv setSeparatorInset:UIEdgeInsetsZero]; } if ([tv respondsToSelector:@selector(setLayoutMargins:)]) { [tv setLayoutMargins:UIEdgeInsetsZero]; } x = 0; w = kSize.width; h = 60; y = kSize.height - h - kNavOffSet - kSafeAreaBottomHeight; UIView* vi; vi = [UIView new]; [vi setFrame:CGRectMake(x, y, w, h)]; [vi setBackgroundColor:backGroundColor]; [self.view addSubview:vi]; NSInteger tag = 0; STButton* stBtn; y = 0; w = 80; x = (kSize.width - w)/2.0; stBtn = [STButton new]; [stBtn setFrame:CGRectMake(x, y, w, h)]; [vi addSubview:stBtn]; arr = @[@"placeHolder",@"发布话题",@"我要记录",@"我要直播",@"我要提问",@"unknown"]; [stBtn setImage:[UIImage imageNamed:@"community2.png"] withTitle:arr[_type.intValue] Font:13 forState:UIControlStateNormal]; [stBtn setTag:tag++]; [stBtn target:self]; [stBtn setStyle:1]; [stBtn setTitleColor:contentTextColor forState:UIControlStateNormal]; UIButton* btn; btn = [[UIButton alloc] initWithFrame:kFrame]; if ( 3 == _type.intValue) { [[UIApplication sharedApplication].keyWindow addSubview:btn]; } [btn setBackgroundColor:[UIColor colorWithWhite:.01 alpha:.5]]; [btn setTag:tag++]; [btn target:self]; moodBg = btn; [moodBg setHidden:YES]; bd = 25; w = kSize.width - bd*2; h = w / 3.0 * 2.77; vi = [[UIView alloc] initWithFrame:CGRectMake(bd, -h, w, h)]; [vi borderColor:[UIColor whiteColor] width:.01 cornorRadios:13]; [btn addSubview:vi]; moodV = vi; [vi setBackgroundColor:[UIColor whiteColor]]; h = w*.3*.57;//.57 + .2 label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, w, h)]; [label setTextAlignment:NSTextAlignmentCenter]; [label setText:@"你是什么心情?"]; [label setTextColor:contentTextColor]; [vi addSubview:label]; btn = [[UIButton alloc] initWithFrame:CGRectMake(w - 40,10,30,30)]; [btn setImage:[UIImage imageNamed:@"drawer_img08.png"] forState:UIControlStateNormal]; [vi addSubview:btn]; [btn setTag:tag++]; [btn target:self]; y += h; w /= 3.0; h = w; NSArray *images; images = @[@"mood0.png",@"mood1.png",@"mood2.png",@"mood3.png",@"mood4.png",@"mood5.png"]; for (int i=0; i 0 && models.count >0) { NSMutableArray *arrs = [NSMutableArray array]; [[models.rac_sequence.signal filter:^BOOL(NSDictionary *dic) { return ![arr containsObject:dic[@"ID"]]; }] subscribeNext:^(NSDictionary *dic) { [arrs addObject:dic]; } completed:^{ models = arrs; dispatch_sync(dispatch_get_main_queue(), ^{ [myTableView reloadData]; }); }]; } }]; [RACObserve(RQ_SHARE_FUNCTION, shieldPeopleIDArr) subscribeNext:^(NSArray *arr) { if (arr.count > 0 && models.count >0) { NSMutableArray *arrs = [NSMutableArray array]; [[models.rac_sequence.signal filter:^BOOL(NSDictionary *dic) { return ![arr containsObject:dic[@"TI_CRUSER"]]; }] subscribeNext:^(NSDictionary *dic) { [arrs addObject:dic]; } completed:^{ models = arrs; dispatch_sync(dispatch_get_main_queue(), ^{ [myTableView reloadData]; }); }]; } }]; } -(void)btnClick:(UIButton*)sender { NSInteger tag = sender.tag; //NSLog(@"btnClick %d",(int)sender.tag); if (0 == tag ) { if (!myDelegate.isLogin) { [LoginViewController loginFromVC:self]; return; } } if (0 == tag && 3 == _type.intValue ) { //show MOOD [moodBg setHidden:NO]; [UIView animateWithDuration:.3 animations:^{ moodV.center = CGPointMake(moodBg.middleX, moodBg.middleY + 39); } completion:^(BOOL finished) { [UIView animateWithDuration:.2 animations:^{ moodV.center = moodBg.center; } completion:^(BOOL finished) { }]; }]; return; } [self hideMood]; if ( 1 == tag || 2 == tag) { //阴影背景。和关闭按钮。 return; } PostTopicVC* vc = [PostTopicVC new]; [vc complete:^{ [self headerRefresh]; }]; vc.type = _type; vc.groupId = _groupId; vc.childID = _childID; vc.mood = tag - 2; [self navPushHideTabbarToVC:vc]; } -(void)hideMood { if (moodBg.hidden) { return; } [UIView animateWithDuration:.3 animations:^{ moodBg.alpha = 0; } completion:^(BOOL finished) { [moodBg setHidden:YES]; [moodBg setAlpha:1]; moodV.y = -moodV.height; }]; } -(void)gotoComment:(NSString *)comId { if (!myDelegate.isLogin) { LoginViewController* vc = [[LoginViewController alloc] init]; vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; vc.modalPresentationStyle = UIModalPresentationFullScreen; [self presentViewController:vc animated:YES completion:nil]; }else{ TopicVC* vc = [[TopicVC alloc] init]; [vc setTopicId:comId]; [vc setTextViewActive:YES]; [self navPushHideTabbarToVC:vc]; } } #pragma mark 表的编辑 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return communityCellHeight(models[indexPath.row] , 0); } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return models.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { CommunityCell* cell = [CommunityCell cellForTableView:tableView Style:0]; //用于通知VC跳转到评论 [cell setDelegate:self]; if (models.count > 0) { [cell setModel:models[indexPath.row]];//崩溃 index 3 beyond bounds for empty array,出现次数3次 if ([[models[indexPath.row] objectForKey:@"TI_TYPE"] integerValue] == 4) { if ([[models[indexPath.row] objectForKey:@"TI_IS_FINISH"] integerValue] > 0) { cell.acceptLabel.text = @"已解决"; cell.acceptLabel.textColor = defGreen; cell.acceptLabel.layer.borderWidth = 1; cell.acceptLabel.layer.borderColor = defGreen.CGColor; }else{ cell.acceptLabel.text = @"未解决"; cell.acceptLabel.textColor = [UIColor redColor]; cell.acceptLabel.layer.borderWidth = 1; cell.acceptLabel.layer.borderColor = [[UIColor redColor] CGColor]; } }else{ cell.acceptLabel.hidden = YES; } } return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:NO]; NSDictionary* dict = models[indexPath.row]; TopicVC* vc = [[TopicVC alloc] init]; [vc setTopicId:dict[@"ID"]]; [self navPushHideTabbarToVC:vc]; } - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsZero]; } if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsZero]; } } #pragma mark - 下拉刷新,上拉加载 - -(void)setRefreshAction{ // 下拉刷新 __weak typeof(self) weakSelf = self; MJRefreshNormalHeader *mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ [weakSelf headerRefresh]; }]; myTableView.mj_header = mj_header; MJRefreshBackNormalFooter *mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{ [weakSelf footerRefresh]; }]; myTableView.mj_footer = mj_footer; } -(void)headerRefresh{ //设置获取数据的方式 _getDataType=MyGetDataTypeHeaderRefresh; //加载数据 [self getModelsAsyc]; } -(void)footerRefresh{ //设置获取数据的方式 _getDataType=MyGetDataTypeFooterRefresh; //加载数据 [self getModelsAsyc]; } #pragma mark cell代理 -(void)didPoint:(NSDictionary *)dict { // NSLog(@"didPoint->%@",dict); [self uploadTopicCommentPoints:dict]; } -(void)uploadTopicCommentPoints:(NSDictionary*)obj { if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableArray* arr = [NSMutableArray array]; [arr addPro:@"user" Value:defUser.userDict[@"id"]]; [arr addPro:@"topicId" Value:obj[@"ID"]]; NSString* method = @"uploadTopicCommentPoints"; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary *root) { if (!root) { ShowMsgFailed(); return; } }]; } -(void)didDePoint:(NSDictionary *)dict { // NSLog(@"didDePoint->%@",dict); [self deleteTopicCommentPoints:dict]; } -(void)deleteTopicCommentPoints:(NSDictionary*)obj { if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableArray* arr = [NSMutableArray array]; [arr addPro:@"user" Value:defUser.userDict[@"id"]]; [arr addPro:@"topicId" Value:obj[@"ID"]]; NSString* method = @"deleteTopicCommentPoints"; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary *root) { if (!root) { ShowMsgFailed(); return; } }]; } #pragma mark - -(void)getModelsAsyc { if (_IS_LOADING) { return; } _IS_LOADING=YES; NSInteger needLoadPage; needLoadPage=_currentPageNum+1; if (_getDataType==MyGetDataTypeHeaderRefresh) { needLoadPage=1; }else if (_getDataType == MyGetDataTypeNomal){ [MBProgressHUD showLoadToView:self.view]; } //-1就是驾校。 if ([_groupId isEqualToString:@"-1"]) { [self getTopicByJxbhWithPage:needLoadPage]; }else{ [self getTopicInfosWithPage:needLoadPage]; } } -(void)getTopicInfosWithPage:(NSInteger)needLoadPage { NSMutableArray *arr=[NSMutableArray array]; [arr addPro:@"groupId" Value:_groupId]; [arr addPro:@"type" Value:_type]; [arr addPro:@"crUser" Value:defUser.userDict[@"id"]]; [arr addPro:@"isPage" Value:@"1"]; [arr addPro:@"pageSize" Value:@"7"]; [arr addPro:@"currentPage" Value:[NSString stringWithFormat:@"%d",(int)needLoadPage]]; if ([_type isEqualToString:@"2"]) { [arr addPro:@"childGroupId" Value:_childID]; } NSString* method = @"getTopicInfos"; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * dict) { _IS_LOADING = NO; if (_getDataType == MyGetDataTypeNomal) { [MBProgressHUD hideHUDForView:self.view]; }else if (_getDataType == MyGetDataTypeFooterRefresh){ [myTableView.mj_footer endRefreshing]; }else{ [myTableView.mj_header endRefreshing]; } if (!dict) { ShowMsgFailed(); return; } if ( [dict[@"code"] isEqualToString:@"1"]) { ShowMsg(dict[@"body"]); return; } NSArray *arr = dict[@"body"]; if (arr.count > 0) { if (_getDataType == MyGetDataTypeHeaderRefresh){ [models removeAllObjects]; } [[arr.rac_sequence.signal filter:^BOOL(NSDictionary *dic) { return ![RQ_SHARE_FUNCTION.shieldTopicIDArr containsObject:dic[@"ID"]] && ![RQ_SHARE_FUNCTION.shieldPeopleIDArr containsObject:dic[@"TI_CRUSER"]] ; }] subscribeNext:^(NSDictionary *dic) { [models addObject:dic]; } completed:^{ dispatch_sync(dispatch_get_main_queue(), ^{ [myTableView reloadData]; _currentPageNum=needLoadPage; _getDataType=MyGetDataTypeNomal; [lblTopicCnt setText:[NSString stringWithFormat:@"话题 %d",(int)models.count]]; }); }]; }else{ ShowMsg(@"已加载全部"); } }]; } -(void)getTopicByJxbhWithPage:(NSInteger)needLoadPage { if ([_jxbh isEqualToString:@""]) { _jxbh = defUser.userSchoolId; } NSMutableArray *arr=[NSMutableArray array]; [arr addPro:@"jxbh" Value: _jxbh]; [arr addPro:@"isPage" Value:@"1"]; [arr addPro:@"pageSize" Value:@"10"]; [arr addPro:@"currentPage" Value:[NSString stringWithFormat:@"%d",(int)needLoadPage]]; NSString* method = @"getTopicByJxbh"; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * dict) { _IS_LOADING = NO; if (_getDataType == MyGetDataTypeNomal) { [MBProgressHUD hideHUDForView:self.view]; }else if (_getDataType == MyGetDataTypeFooterRefresh){ [myTableView.mj_footer endRefreshing]; }else{ [myTableView.mj_header endRefreshing]; } if (!dict) { ShowMsgFailed(); return; } if ( [dict[@"code"] isEqualToString:@"1"]) { ShowMsg(dict[@"body"]); return; } NSArray *arr = dict[@"body"]; if (arr.count > 0) { if (_getDataType==MyGetDataTypeHeaderRefresh){ [models removeAllObjects]; } [[arr.rac_sequence.signal filter:^BOOL(NSDictionary *dic) { return ![RQ_SHARE_FUNCTION.shieldTopicIDArr containsObject:dic[@"ID"]] && ![RQ_SHARE_FUNCTION.shieldPeopleIDArr containsObject:dic[@"TI_CRUSER"]] ; }] subscribeNext:^(NSDictionary *dic) { [models addObject:dic]; } completed:^{ dispatch_sync(dispatch_get_main_queue(), ^{ [myTableView reloadData]; _currentPageNum=needLoadPage; _getDataType=MyGetDataTypeNomal; [lblTopicCnt setText:[NSString stringWithFormat:@"话题 %d",(int)models.count]]; }); }]; }else{ ShowMsg(@"已加载全部"); } }]; } @end