123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599 |
- /**采取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 ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate,CommunityCellDelegate>
- {
- //全局的滚动视图
- 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<titles.count; i++)
- {
- STButton* btn = [[STButton alloc] initWithFrame:SGRectMake(w*i, 0, w, h)];//h=96
- [btn setImage:[UIImage imageNamed:images[i]] withTitle:titles[i] Font:NormalFont forState:UIControlStateNormal];
- [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
- [btn setTag:tag++];//1-count
- [sv addSubview:btn];
- if (str && 0 == i) {
- if (str.length > 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
|