You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

221 lines
7.1 KiB

//
// CommProViewController.m
// tongxin
//
// Created by WeiChaoZheng on 2018/10/15.
// Copyright © 2018年 xTT. All rights reserved.
//
#import "CommProViewController.h"
#import "CQCollectionViewCell.h"
#import "QDetailViewController.h"
#import "AllQViewController.h"
@interface CommProViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong, nonatomic) NSMutableArray *collectionTitleDataSource;
@property (strong, nonatomic) NSMutableArray *collectionDataSource;
@property (weak, nonatomic) IBOutlet UIView *titleTempView;
@end
@implementation CommProViewController
@synthesize myDataSource = _myDataSource;
-(NSMutableArray *)collectionTitleDataSource{
if(!_collectionTitleDataSource){
_collectionTitleDataSource = [NSMutableArray array];
}
return _collectionTitleDataSource;
}
-(NSMutableArray *)collectionDataSource{
if(!_collectionDataSource){
_collectionDataSource = [NSMutableArray array];
}
return _collectionDataSource;
}
- (NSMutableArray *)myDataSource{
if (!_myDataSource) {
_myDataSource = [[NSMutableArray alloc] init];
}
return _myDataSource;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = tabViewBG;
self.myTableView.bounces = NO; // 没有弹性
self.myTableView.layer.cornerRadius = 5;
self.myTableView.layer.masksToBounds = YES;
self.titleTempView.backgroundColor = mainColor;
[self.bgScrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view);
make.top.equalTo(self.view).offset(iPhoneX_NavHeight);
make.bottom.equalTo(self.view.mas_bottom);
}];
self.zx_navTitle = @"常见问题";
[self loadData];
[self initCollectionView];
}
-(void)initCollectionView{
//注册 cell
[self.CQcollectionView registerNib:[UINib nibWithNibName:@"CQCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"CQCollectionViewCellID"];
self.CQcollectionView.layer.cornerRadius = 3;
self.CQcollectionView.layer.masksToBounds = YES;
}
-(void)loadData{
WEAKSELF
[User getCategoryQAndHotQSuccess:^(id responseObject) {
//热门问题
NSArray * titles = responseObject[@"titles"];
NSMutableArray *arr = [NSMutableArray array];
for (int i = 0; i < titles.count; i++) {
if(i<3){
[arr addObject:titles[i]];
}else{
break;
}
}
if(arr.count < 3){
weakSelf.tableHeightNSLC.constant = arr.count * 50;
}else{
weakSelf.tableHeightNSLC.constant = 3 * 50;
}
[weakSelf.myDataSource addObject:arr];
[weakSelf.myTableView reloadData];
//分类别
NSArray * categorys = responseObject[@"categorys"];
[weakSelf setDataWithCollection:categorys];
[weakSelf.CQcollectionView reloadData];
} failure:^(id faiObject) {
}];
}
/**
更多的 问题
*/
- (IBAction)moreQHot:(UIButton *)sender {
AllQViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"AllQViewController"];
[self.navigationController pushViewController:vc animated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(section == 0){
return [self.myDataSource[section] count];
}else{
return 1;
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 49;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *dic = self.myDataSource[indexPath.section][indexPath.row];
baseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"baseCell"];
if (!cell) {
cell = [[baseCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"baseCell"];
cell.textLabel.text = dic[@"title"];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *dic = self.myDataSource[indexPath.section][indexPath.row];
NSString *content = dic[@"content"];
NSString *titleStr = dic[@"title"];
QDetailViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"QDetailViewController"];
vc.content = content;
vc.titleStr = titleStr;
[self.navigationController pushViewController:vc animated:YES];
}
-(void)setDataWithCollection:(NSArray*)data{
[self.collectionDataSource removeAllObjects];
[self.collectionTitleDataSource removeAllObjects];
for (int i = 0; i < data.count; i++) {
NSDictionary *dic = data[i];
[self.collectionTitleDataSource addObject: dic[@"category"]];
[self.collectionDataSource addObject:dic[@"categoryid"]];
}
[self.CQcollectionView reloadData];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.collectionTitleDataSource.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake((ScreenWidth - 14*2 - 4)/3, 53);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(0, 1, 0, 1);
}
//设置水平间距 (同一行的cell的左右间距)
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 1;
}
//垂直间距 (同一列cell上下间距)
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CQCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CQCollectionViewCellID" forIndexPath:indexPath];
[cell.clickBtn setTitleColor:[self getColorWithIndex:indexPath.row] forState:0];
[cell.clickBtn setTitle:self.collectionTitleDataSource[indexPath.item] forState:0];
cell.categoryid = self.collectionDataSource[indexPath.item];
return cell;
}
//从 6种颜色中取值
-(UIColor*)getColorWithIndex:(NSInteger)index{
if(index >= 6){
index -= 6;
}
NSArray *arr = @[RGB(253,98,0),
RGB(30,201,189),
RGB(0,174,214),
RGB(92,118,207),
RGB(251,52,53),
RGB(0,119,206),
];
return arr[index];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end