//
// GameScoreListViewController.m
// HengTaiXinGolf
//
// Created by 欧阳荣 on 15/9/16.
// Copyright (c) 2015年 HengTaiXinGolf. All rights reserved.
//
#import "GameScoreListViewController.h"
#import "DetaiScoreViewController.h"
#define CELL_GAMESCORE @"gameScoreCell"
@interface GameScoreListViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchResultsUpdating,MBProgressHUDDelegate>
@property (nonatomic,strong) NSMutableArray *searchList;
@property (nonatomic,strong) UISearchController *searchController;
@end
@implementation GameScoreListViewController
{
NSMutableArray * _scoreArr;
NSString *_paramStr;
BOOL _searchYN;
MBProgressHUD *HUD;
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil];
if (self)
{
_scoreArr = [NSMutableArray array];
self.searchList = [NSMutableArray array];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
// [self creatBackItem];
[self createBackBtn];
_searchYN = NO;
[self geturl:KgetGameScoreList];
[self creatMainView];
}
-(void)createBackBtn{
//自定义导航栏
UIImageView *navImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 64)];
navImgView.image = [UIImage imageNamed:@"top_bg"];
[self.view addSubview:navImgView];
//自定义导航栏标题
self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(IPHONE_WIDTH/2-100, 5 , 200, 64)];
self.titleLabel.text = @"记分卡";
self.titleLabel.backgroundColor = [UIColor clearColor]; //设置Label背景透明
self.titleLabel.font = [UIFont boldSystemFontOfSize:20]; //设置文本字体与大小
self.titleLabel.textColor = [UIColor whiteColor]; //设置文本颜色
self.titleLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.titleLabel];
//返回键
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
backBtn.frame = CGRectMake(6, 64/7 + 6, 40, 64*5/7);
[backBtn setImage:[UIImage imageNamed:@"title_bar_back"] forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(popViewController) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backBtn];
}
-(void)popViewController{
NSLog(@"注册返回");
[self dismissViewControllerAnimated:NO completion:nil];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
}
-(void)creatMainView{
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, IPHONE_WIDTH, IPHONE_HEIGHT - 64)];
self.tableView.bounces = YES;
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView registerClass:[GameScoreListCell class] forCellReuseIdentifier:CELL_GAMESCORE];
[self.view addSubview:self.tableView];
//添加搜索框
self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.delegate = self;
self.searchController.searchBar.placeholder = @"搜索";
self.searchController.searchBar.barTintColor = [UIColor colorWithRed:231/255.0 green:231/255.0 blue:231/255.0 alpha:1.0];
[self.searchController.searchBar sizeToFit];
self.definesPresentationContext = NO;
//设置为NO,可以点击搜索出来的内容
_searchController.dimsBackgroundDuringPresentation = NO;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
-(void)geturl:(NSString *)url
{
NSDictionary *parameter;
if (_searchYN) {
parameter = @{@"memberId":[UserModel sharedInstance].memberId,@"paramStr":_paramStr,@"pageNum":@0,@"pageSize":@50,@"appId":@"ET12412",@"appSecret":@"4124bc0a9335c27f086f24ba207a4912"};
}else{
parameter = @{@"memberId":[UserModel sharedInstance].memberId,@"pageNum":@0,@"pageSize":@50,@"appId":@"ET12412",@"appSecret":@"4124bc0a9335c27f086f24ba207a4912"};
}
[HTTPRequestTool sendGetAFRequest:url withParameters:parameter withSuccess:^(id message) {
NSDictionary *tempArr = [NSJSONSerialization JSONObjectWithData:message options:NSJSONReadingMutableContainers error:nil];
NSLog(@"====我的记分卡列表请求数据tempArr ==== %@",tempArr);
/**
* 请求数据 (判断数据请求的类型)
*/
if (_searchYN) {
[_searchList removeAllObjects];
for (NSDictionary *tempDic in tempArr[@"gameScoreList"]) {
GameScoreListModel *gameModel = [[GameScoreListModel alloc]init];
[gameModel setValuesForKeysWithDictionary:tempDic];
[self.searchList addObject:gameModel];
}
if ([_searchList count] == 0) {
HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.color = [UIColor grayColor];
HUD.labelText = @"搜索结果为空!";
[HUD hide:YES afterDelay:3];
HUD.delegate = self;
}
}else{
[_scoreArr removeAllObjects];
for (NSDictionary *tempDic in tempArr[@"gameScoreList"]) {
GameScoreListModel *gameModel = [[GameScoreListModel alloc]init];
[gameModel setValuesForKeysWithDictionary:tempDic];
[_scoreArr addObject:gameModel];
}
}
[self.tableView reloadData];
} andWithFail:^(id message) {
NSLog(@"error = %@",message);
}];
}
#pragma mark-------------------tableViewDelegate----------------
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100.0;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (self.searchController.active) {
return [self.searchList count];
}else{
return [_scoreArr count];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
GameScoreListCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_GAMESCORE];
if (cell == nil) {
cell = [[GameScoreListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELL_GAMESCORE];
}
if (self.searchController.active) {
if ([self.searchList count] > 0) {
GameScoreListModel *model = self.searchList[indexPath.row];
[cell fillCellWithModel:model];
}
}else{
if ([_scoreArr count] > 0 ) {
GameScoreListModel *model = _scoreArr[indexPath.row];
[cell fillCellWithModel:model];
}
}
//取消点击效果
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (self.searchController.active) {
if ([self.searchList count] > 0 ) {
GameScoreListModel *model = self.searchList[indexPath.row];
DetaiScoreViewController *detailVC = [[DetaiScoreViewController alloc]init];
detailVC.branchName = model.branchName;
detailVC.scoreNo = model.scoreNo;
[self.searchController presentViewController:detailVC animated:NO completion:nil];
}
}else{
if ([_scoreArr count] > 0 ) {
GameScoreListModel *model = _scoreArr[indexPath.row];
DetaiScoreViewController *detailVC = [[DetaiScoreViewController alloc]init];
detailVC.branchName = model.branchName;
detailVC.scoreNo = model.scoreNo;
[self presentViewController:detailVC animated:NO completion:nil];
}
}
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
_paramStr = self.searchController.searchBar.text;
_searchYN = YES;
[self geturl:KgetGameScoreList];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
}
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
if (self.searchController.active) {
NSLog(@"搜索框有数据就及时刷新");
}else{
_searchYN = NO;
[self geturl:KgetGameScoreList];
}
}
//创建返回按钮
-(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 {
// UIViewController *cvc = [[UIViewController alloc]init];
// cvc.view.tag = 1;
// NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:cvc,@"Hidden", nil];
//
// NSNotification *notification = [NSNotification notificationWithName:@"tongzhiHidden" object:nil userInfo:dict];
// [[NSNotificationCenter defaultCenter]postNotification:notification];
//
[self.navigationController popToRootViewControllerAnimated:NO];
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (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