//
// LocationTeamViewController.h
// HengTaiXinGolf
//
// Created by 欧阳荣 on 16/4/18.
// Copyright © 2016年 HengTaiXinGolf. All rights reserved.
//
#import "ParentsViewController.h"
@interface LocationTeamViewController : ParentsViewController
@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic ,copy) NSIndexPath * selectedIndex;
@property (nonatomic) BOOL isOpen;
@end
//
// LocationTeamViewController.m
// HengTaiXinGolf
//
// Created by 欧阳荣 on 16/4/18.
// Copyright © 2016年 HengTaiXinGolf. All rights reserved.
//
#import "LocationTeamViewController.h"
#import "ProCityModel.h"
#import "ProvinceCell.h"
#import "ProvinceListModel.h"
#define PROVINCECELL @"ProvinceCell"
@interface LocationTeamViewController ()<UITableViewDataSource,UITableViewDelegate>
@end
@implementation LocationTeamViewController
{
NSMutableArray * _proListArr;
NSMutableArray * _cityArr;
ProvinceListModel * _selectModel;
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil];
if (self)
{
_proListArr = [NSMutableArray array];
_cityArr = [NSMutableArray array];
_isOpen = YES;
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0 , 100, 44)];
titleLabel.backgroundColor = [UIColor clearColor]; //设置Label背景透明
titleLabel.font = [UIFont boldSystemFontOfSize:KTitle]; //设置文本字体与大小
titleLabel.textColor = [UIColor whiteColor]; //设置文本颜色
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.text = @"所在地"; //设置标题
self.navigationItem.titleView = titleLabel;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
[self createTableView];
[self geturl:KgetProvinceCity];
[self creatBackItem];
}
-(void)createTableView{
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT) style:UITableViewStylePlain];
[self.tableView registerClass:[ProvinceCell class] forCellReuseIdentifier:PROVINCECELL];
self.tableView.delegate = self;
self.tableView.dataSource = self;
UIView *vv=[[UIView alloc]init];
vv.backgroundColor=[UIColor whiteColor];
vv.frame = CGRectZero;
self.tableView.tableFooterView = vv;
[self.view addSubview:self.tableView];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == _selectedIndex.row && _selectedIndex != nil) {
//选中的cell的高度.ceil函数,向上取整。
// float f = 1.5; int a; a = ceil(f); NSLog("a = %d",a);
// 输出结果是2。ceil()方法是向上取整,取得不小于浮点数的最小整数,对于正数来说是舍弃浮点数部分并加1,对于复数来说就是舍弃浮点数部分.
if (_isOpen == YES) {
NSInteger a = [_selectModel.arrayCity count]/3;
NSInteger b = [_selectModel.arrayCity count]%3;
if (b > 0) {
a = a + 1;
}
//让cell展开状态
return 88 * KSCALE_X * (a + 1);
}else{
//不让cell展开
return 88 * KSCALE_X;
}
}else{
//未选中的cell的高度
return 88 * KSCALE_X;
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [_proListArr count];
}
-(UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
ProvinceCell * cell = [tableView dequeueReusableCellWithIdentifier:PROVINCECELL];
if (!cell) {
cell = [[ProvinceCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PROVINCECELL];
}else{
[cell removeFromSuperview];
cell = nil;
cell = [[ProvinceCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PROVINCECELL];
}
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
ProvinceListModel * model = _proListArr[indexPath.row];
// DSLog(@" --------- cell ------- %ld",(long)indexPath.row);
if (indexPath.row == _selectedIndex.row && _selectedIndex != nil) {
//如果是展开的
if (_isOpen == YES) {
NSInteger j;
NSInteger k;
// 180 60
CGFloat margin_w = ((640 - 180 * 3)/4)*KSCALE_X;
CGFloat margin_H = ((88 - 60)/2) * KSCALE_X;
CGFloat btn_W = 180 * KSCALE_X;
CGFloat btn_H = 60 * KSCALE_X;
NSInteger X = [_selectModel.arrayCity count]/3;
NSInteger Y = [_selectModel.arrayCity count]%3;
if (Y > 0) {
X = X + 1;
}
cell.titleLab.text = model.proName;
UIView * cityView = [[UIView alloc]initWithFrame:CGRectMake(0, 88 * KSCALE_X, IPHONE_WIDTH, 88 * KSCALE_X * (X + 1) - 88 * KSCALE_X)];
cityView.backgroundColor = kColor(231, 231, 231);
[cell.contentView addSubview:cityView];
for (NSInteger i = 0; i < [_selectModel.arrayCity count]; i ++) {
UIButton * cityBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
j = i/3;//行 0 1 2
k = i%3;//列 0 1 2
cityBtn.frame = CGRectMake(margin_w + k * (btn_W + margin_w) ,margin_H + j * (btn_H +2 * margin_H), btn_W, btn_H);
ProCityModel * model = _cityArr[i];
[cityBtn setTitle:model.cityName forState:UIControlStateNormal];
cityBtn.tag = i;
[cityBtn addTarget:self action:@selector(selecteCity) forControlEvents:UIControlEventTouchUpInside];
[cityBtn.layer setBorderWidth:1];//设置边界的宽度
//设置按钮的边界颜色
[cityBtn.layer setBorderColor:kColor(205, 204, 209).CGColor];
[cityBtn.layer setBorderWidth:1];
[cityBtn.layer setMasksToBounds:YES];//设置按钮的圆角半径不会被遮挡
cityBtn.backgroundColor = [UIColor whiteColor];
[cityBtn setTitleColor:kColor(0, 0, 0) forState:UIControlStateNormal];
[cityView addSubview:cityBtn];
}
cell.accessImgView.frame = CGRectMake(IPHONE_WIDTH - 44 * KSCALE_X, 35 * KSCALE_X, 25*KSCALE_X, 18 * KSCALE_X);
cell.accessImgView.image = [UIImage imageNamed:@"xxjt"];//xxjt
}else{
cell.titleLab.text = model.proName;
cell.accessImgView.frame = CGRectMake(IPHONE_WIDTH - 44 * KSCALE_X, 35 * KSCALE_X, 18*KSCALE_X, 25 * KSCALE_X);
cell.accessImgView.image = [UIImage imageNamed:@"jt_S"];//xxjt
}
//不是自身
}else{
cell.accessImgView.frame = CGRectMake(IPHONE_WIDTH - 44 * KSCALE_X, 35 * KSCALE_X, 18*KSCALE_X, 25 * KSCALE_X);
cell.titleLab.text = model.proName;
cell.accessImgView.image = [UIImage imageNamed:@"jt_S"];//xxjt
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//将索引加到数组中
NSArray * indexPaths = [NSArray arrayWithObject:indexPath];
//判断选中不同的row状态时候
_selectModel = _proListArr[indexPath.row];
[_cityArr removeAllObjects];
for (NSDictionary * tempDic in _selectModel.arrayCity) {
ProCityModel * model = [[ProCityModel alloc]init];
[model setValuesForKeysWithDictionary:tempDic];
[_cityArr addObject:model];
}
if (self.selectedIndex != nil && indexPath.row == _selectedIndex.row) {
_isOpen = !_isOpen;
}else if (self.selectedIndex != nil && indexPath.row != _selectedIndex.row){
indexPaths = [NSArray arrayWithObjects:indexPath,_selectedIndex, nil];
_isOpen = YES;
}
//记下选中的索引
self.selectedIndex = indexPath;
//刷新
[tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
// DSLog(@" indexPaths %@ indexPath %@ _selectedIndex.row%ld",indexPaths,indexPath,(long)_selectedIndex.row);
}
-(void)selecteCity{
DSLog(@"选择城市");
}
-(void)geturl:(NSString *)url
{
NSString *memStr = [UserModel sharedInstance].memberId;
//判断是否好友请求参数
NSMutableDictionary * _param = [NSMutableDictionary dictionary];
[_param setValue:memStr forKey:@"memberId"];
[_param setValue:KAPPID forKey:@"appId"];
[_param setValue:KappSecret forKey:@"appSecret"];
[_param setValue:[CommonTools getTimeScamp] forKey:@"timestamp"];
NSArray * paramArr = @[[NSString stringWithFormat:@"memberId%@",_param[@"memberId"]],[NSString stringWithFormat:@"appId%@",_param[@"appId"]],[NSString stringWithFormat:@"appSecret%@",_param[@"appSecret"]],[NSString stringWithFormat:@"timestamp%@",_param[@"timestamp"]]];
[_param setValue:[CommonTools tokenmd5StringFromArray:paramArr] forKey:@"digest"];
[HTTPRequestTool sendGetAFRequest:url withParameters:_param withSuccess:^(id message) {
NSDictionary *tempArr = [NSJSONSerialization JSONObjectWithData:message options:NSJSONReadingMutableContainers error:nil];
DSLog(@" 所在地 %@",tempArr);
/**
* 请求数据 (判断数据请求的类型)
*/
for (NSDictionary * tempDic in tempArr[@"provinceList"]) {
ProvinceListModel * model = [[ProvinceListModel alloc]init];
[model setValuesForKeysWithDictionary:tempDic];
[_proListArr addObject:model];
}
[self.tableView reloadData];
} andWithFail:^(id message) {
DSLog(@"error = %@",message);
}];
}
//创建返回按钮
-(void)creatBackItem{
// CGFloat frameY = 0.0f;
UIBarButtonItem * leftItem = nil;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
// frameY = 0.0f;
leftItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"title_bar_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(returnClicked)];
} else {
// frameY = 0.0f;
leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"title_bar_back"] style:UIBarButtonItemStyleDone target:self action:@selector(returnClicked)];
}
self.navigationItem.leftBarButtonItem = leftItem;
}
-(void)returnClicked {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end