/**采取aspectFill来实现适配如何。 问题又来了。有时,需要固定高度怎么办。有时不需要。 不对啊。fill是可以实现屏幕等比缩放的啊。 */ #import "CommunityVC.h" #import "STButton.h" #import "CommunityCell.h" #import "TopicVC.h" #import "TopicesVC.h" #import "PostTopicVC.h" #import "DiaryVC.h" #import "LoginViewController.h" #import "MJRefresh.h" typedef NS_ENUM(NSInteger, MyGetDataType) { //正常请求数据 MyGetDataTypeNomal=0, //下拉刷新请求数据 MyGetDataTypeHeaderRefresh, //上拉加载更多请求数据 MyGetDataTypeFooterRefresh }; @interface CommunityVC () { //全局的滚动视图 UIScrollView* scroll; UIView* header; //头部的按钮标题。保存判断 NSMutableArray* titles; UIPageControl* pageCtl; UITableView* myTableView; NSMutableArray* models; /**点击后滚动到头部的按钮 */ UIButton* gotoTopBtn; //记录页数 NSInteger _currentPageNum; //正在加载的状态 BOOL _IS_LOADING; //加载数据的类型 MyGetDataType _getDataType; BOOL myschool;//是否存在我的驾校 } @end @implementation CommunityVC - (void)viewDidLoad { [super viewDidLoad]; [self myInit]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; if (_currentPageNum == 0) { [self getTopicInfos]; } } -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } #pragma mark - /**注意这里是导航栏带来的Y值偏移 */ -(void)myInit { [self configNavigationBar]; self.navigationController.navigationBar.translucent = NO; [self.view setBackgroundColor:[UIColor whiteColor]]; [self setTitle:@"驾考社区"]; models = [NSMutableArray array]; _currentPageNum = 0; _IS_LOADING = NO; _getDataType = MyGetDataTypeNomal; CGFloat x,y,w,h,bd; UIBarButtonItem* backBbi ; backBbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"community3.png"] style:UIBarButtonItemStylePlain target:self action:@selector(btnClick:)]; [backBbi setTintColor:defGreen]; [backBbi setTag:-1]; [self.navigationItem setRightBarButtonItem:backBbi]; header = [[UIView alloc] initWithFrame:SGRectMake(0, 0, 320, 100)]; x=0; w = kSize.width; y=0; h= kSize.height - kNavOffSet; NSInteger tag = 0; UIScrollView* sv = [[UIScrollView alloc] initWithFrame:CGRectMake(x, y, w, h)]; [header addSubview:sv]; scroll = sv; //最好是。它们的tag值不要变。 NSArray* arr = @[@"吐槽",@"晒图",@"找驾校",@"科目一",@"科目二",@"科目三",@"科目四",@"下证",@"许愿"];//@"最新话题", titles = [NSMutableArray arrayWithArray:arr]; arr = @[@"community_img02.png",@"community_img03.png",@"community_img04.png",@"community_img05.png" ,@"community_img07.png",@"community_img08.png",@"community_img09.png",@"community_img10.png",@"subject2_standart_icon.png"];//@"community_img01.png", NSMutableArray* images = [NSMutableArray arrayWithArray:arr]; //如果有我的驾校 就在这里插入我的驾校信息 NSString* str = [defUser.userSchDict objectForKey:@"shortName"]; myschool = NO; if (str && str.length > 0) { myschool = YES; [titles insertObject:str atIndex:0]; [images insertObject:@"learn_img03.png" atIndex:0]; } w = 320 / 5.0; h = w*1; sv = [[UIScrollView alloc] initWithFrame:SGRectMake(0, 0, 320, h)]; [sv setTag:0]; [header addSubview:sv]; sv.scrollEnabled = YES; sv.contentSize = SGSizeMake(w*titles.count, 0); sv.showsHorizontalScrollIndicator = NO; sv.pagingEnabled = YES; [sv setDelegate:self]; tag += 1; for (int i = 0; i 4) { [btn setTitle:[titles[0] substringToIndex:4] forState:UIControlStateNormal]; [btn setSubTitle:[str substringFromIndex:4] font:NormalFont color:kTitleColor]; } } } UIPageControl* page = [[UIPageControl alloc] initWithFrame:SGRectMake(0, h - 20, 320, 20)]; [header addSubview:page]; page.currentPageIndicatorTintColor = defGreen; page.pageIndicatorTintColor = KlineColor; // if (myShcool == NO) { // page.numberOfPages = 2; // [sv setContentSize:CGSizeMake(kSize.width * 2, 0)]; // }else{ // page.numberOfPages = 3; // [sv setContentSize:CGSizeMake(kSize.width * 3, 0)]; // } page.numberOfPages = 2; [sv setContentSize:CGSizeMake(kSize.width * 2, 0)]; page.userInteractionEnabled = NO; pageCtl = page; y = h ; h = 25; UILabel *label; label = [[UILabel alloc] initWithFrame:SGRectMake(20, y, 320 -20, h)]; [label setText:@"热门话题"]; [label setTextColor:contentTextColor]; [header addSubview:label]; [label addViewWithRect:SGRectMake(0, y+h -1, 320, 1)]; header.frame = SGRectMake(0, 0, 320 , y+h); UITableView* tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kSize.width, kSize.height -kNavOffSet-kSafeAreaBottomHeight)]; [self.view addSubview:tv]; [tv setDelegate:self]; [tv setDataSource:self]; tv.tableHeaderView = header; tv.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; [tv setTag:1]; if ([tv respondsToSelector:@selector(setSeparatorInset:)]) { [tv setSeparatorInset:UIEdgeInsetsZero]; } if ([tv respondsToSelector:@selector(setLayoutMargins:)]) { [tv setLayoutMargins:UIEdgeInsetsZero]; } myTableView = tv; //刷新 [self setRefreshAction]; UIButton* btn ; bd = 15; h = w = 35; x = kSize.width - w - bd; y = kSize.height - 2* w - kNavOffSet; btn = [[UIButton alloc] init]; [btn setFrame:CGRectMake(x, y, w, h)]; [btn setImage:[UIImage imageNamed:@"community5.png"] forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [btn setFrame:CGRectMake(x, y, w, h)]; [btn setBackgroundColor:defGreen]; [btn borderColor:defGreen width:1 cornorRadios:w/2.0]; [self.view addSubview:btn]; [btn setTag:-3]; y -= bd+h; btn = [[UIButton alloc] init]; [btn setFrame:CGRectMake(x, y, w, h)]; [btn setImage:[UIImage imageNamed:@"location_more_arrow.png"] forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [btn setBackgroundColor:[UIColor whiteColor]]; [btn borderColor:KlineColor width:1 cornorRadios:w/2.0]; [self.view addSubview:btn]; btn.transform = CGAffineTransformMakeRotation(M_PI); [btn setTag:-2]; gotoTopBtn = btn; btn.hidden = YES; [RACObserve(RQ_SHARE_FUNCTION, shieldTopicIDArr) 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[@"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]; }); }]; } }]; } /**这里有个很有意思的事情。self已经present到vc了。可是self.nav却push到其他地方了。--尚未发现问题--danson */ -(void)btnClick:(UIButton*)sender { NSInteger tag = sender.tag; if (-3 == tag ||-1 == tag ) { if (!myDelegate.isLogin) { [LoginViewController loginFromVC:self]; return; } } if (-3 == sender.tag) { PostTopicVC* vc = [[PostTopicVC alloc] init]; [vc complete:^{ [models removeAllObjects]; _currentPageNum = 0; //viewdidappear里面会刷新 }]; vc.type = @"1"; [self navPushHideTabbarToVC:vc]; } if (-2 == sender.tag) { [UIView animateWithDuration:.5 animations:^{ gotoTopBtn.alpha = 0; } completion:^(BOOL finished) { [gotoTopBtn setHidden:YES]; }]; [myTableView setContentOffset:CGPointMake(0, 0) animated:YES]; } if (-1 == sender.tag) { DiaryVC* vc = [[DiaryVC alloc] init]; vc.userID = defUser.userDict[@"id"]; [self navPushHideTabbarToVC:vc]; } //这里要判断一下点击了那个按钮。接口里面没有最新话题。算了。最新话题。就传个0过去好了。 if (0 <= sender.tag) { TopicesVC* vc = [[TopicesVC alloc] init]; vc.type = @"1"; if (myschool == NO) { //没有驾校. 1-9 vc.groupId = [NSString stringWithFormat:@"%d",(int)tag]; }else{ //有驾校 0,-1,1-10 // if (0 == tag) {//辽宁“最新话题”已经去掉 // vc.groupId = @"0"; // } if (1 == tag) { vc.groupId = @"-1"; vc.jxbh = @""; vc.jxjc = @""; } if (1 < tag ) { vc.groupId = [NSString stringWithFormat:@"%d",(int)tag-1]; } } [self navPushHideTabbarToVC:vc]; } } -(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]; } } -(void)didPoint:(NSDictionary *)dict { // NSLog(@"didPoint->%@",dict); [self uploadTopicCommentPoints:dict]; } -(void)didDePoint:(NSDictionary *)dict { // NSLog(@"didDePoint->%@",dict); [self deleteTopicCommentPoints:dict]; } #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; //先这样处理吧 我也很绝望啊 //隐藏底部“上拉加载更多” // 设置了底部inset myTableView.contentInset = UIEdgeInsetsMake(0, 0, 30, 0); // 忽略掉底部inset myTableView.mj_footer.ignoredScrollViewContentInsetBottom = 30; } -(void)headerRefresh{ //设置获取数据的方式 _getDataType=MyGetDataTypeHeaderRefresh; //加载数据 [self getTopicInfos]; [myTableView.mj_header endRefreshing]; } -(void)footerRefresh{ //设置获取数据的方式 _getDataType=MyGetDataTypeFooterRefresh; //加载数据 [self getTopicInfos]; [myTableView.mj_footer endRefreshing]; } #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]; [cell setShowTag:YES]; //用于通知VC跳转到评论 [cell setDelegate:self]; [cell setModel:models[indexPath.row]]; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; NSDictionary* dict = models[indexPath.row]; TopicVC* vc = [[TopicVC alloc] init]; [vc setTopicId:dict[@"ID"]]; vc.userId = dict[@"TI_CRUSER"]; [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)getTopicInfos { //判断当前是否正在加载数据。如果正在加载数据,直接return。 if (_IS_LOADING) { return; } if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } _IS_LOADING=YES; //获取第一页数据 NSInteger needLoadPage; needLoadPage=_currentPageNum+1; if (_getDataType==MyGetDataTypeHeaderRefresh) { needLoadPage=1; } NSMutableArray *arr=[NSMutableArray array]; //默认查询的是所有 所以groupId传空值 文档上好像说 -1 是空 danson [arr addPro:@"groupId" Value:@""]; [arr addPro:@"type" Value:@"1"]; [arr addPro:@"crUser" Value:defUser.userDict[@"id"]]; [arr addPro:@"isPage" Value:@"1"]; [arr addPro:@"pageSize" Value:@"10"]; [arr addPro:@"currentPage" Value:[NSString stringWithFormat:@"%ld",(long)needLoadPage]]; //如果话题上传类型type为2 则添加字段 childGroup NSString* method = @"getTopicInfos"; [MBProgressHUD showLoadToView:self.view]; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary *dict) { [MBProgressHUD hideHUDForView:self.view]; _IS_LOADING=NO; //NSLog(@"%@,%@,%@",arr,dict,xml); if (!dict) { ShowMsgFailed(); return; } if ( [dict[@"code"] isEqualToString:@"1"]) { ShowMsg(dict[@"body"]); return; } //处理数据 //如果获取到了数据,而且是下拉刷新,清空数组。 if (_getDataType==MyGetDataTypeHeaderRefresh){ [models removeAllObjects]; } //追加数据 NSArray *arr = dict[@"body"]; [[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(), ^{ if (models.count == 0) { ShowMsg(@"已加载全部"); }else{ //计数器+1 _currentPageNum=needLoadPage; } [myTableView reloadData]; //恢复初始状态。 _getDataType=MyGetDataTypeNomal; }); }]; }]; } -(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; } }]; } -(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)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { if(scrollView.tag > 0) { //tableview CGPoint off = scrollView.contentOffset; if (off.y < 100) { [UIView animateWithDuration:.5 animations:^{ gotoTopBtn.alpha = 0; } completion:^(BOOL finished) { [gotoTopBtn setHidden:YES]; }]; }else if (gotoTopBtn.hidden) { [gotoTopBtn setHidden:NO]; gotoTopBtn.alpha = .2; [UIView animateWithDuration:.7 animations:^{ gotoTopBtn.alpha = 1; } ]; } }else{ //顶部 NSInteger pInd = (int)(scrollView.contentOffset.x / kSize.width); pageCtl.currentPage = pInd% pageCtl.numberOfPages; } } @end