//
// CollectionViewExample.m
// ChartExample
//
// Created by Makarov Roman on 04.03.15.
// Copyright (c) 2015 Makarov Roman. All rights reserved.
//
#import "CollectionViewExample.h"
#import "InfoBankomatView.h"
#import "XMLReader.h"
@interface CollectionViewExample ()
@end
@implementation CollectionViewExample {
NSMutableArray *array;
NSMutableArray *nameImage;
NSMutableArray *numBankomat;
NSMutableArray *nameBankomat;
NSMutableArray *addressBankomat;
NSMutableArray *arr;
}
@synthesize topMenu;
@synthesize textTitle;
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithRed:40.0f/255.0f green:49.0f/255.0f blue:60.0f/255.0f alpha:1.0];
/* Здаем цвет верхнего навигатора */
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:237.0f/255.0f green:49.0f/255.0f blue:36.0f/255.0f alpha:1.0];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
[self.navigationController.navigationBar
setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
self.navigationController.navigationBar.translucent = NO;
/* Устанавливаем название кнопки верхнего меню */
self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:self.textTitle style:UIBarButtonItemStyleBordered target:self action:nil];
/* Добавляем кнопку поиска */
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:nil action:nil];
// Настройки SegmentControl
[self.topMenu setFrame: CGRectMake(35, 200, 250, 50)];
//self.segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
[self.topMenu addTarget:self action:@selector(MySegmentControlAction:)forControlEvents:UIControlEventValueChanged];
[self.topMenu setSelectedSegmentIndex:1];
/* Устанавливаем параметры верхнего меню */
UIFont *font = [UIFont boldSystemFontOfSize:10.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
[self.topMenu setTitleTextAttributes:attributes
forState:UIControlStateNormal];
self.topMenu.layer.borderColor = [UIColor colorWithRed:120.0f/255.0f green:116.0f/255.0f blue:116.0f/255.0f alpha:1.0].CGColor;
/* Инициализация массивов */
array = [[NSMutableArray alloc] init];
nameImage = [[NSMutableArray alloc] init];
numBankomat = [[NSMutableArray alloc] init];
nameBankomat = [[NSMutableArray alloc] init];
addressBankomat = [[NSMutableArray alloc] init];
/* Задаем фоновый цвет контроллера */
collectionView.backgroundColor = [UIColor colorWithRed:40.0f/255.0f green:49.0f/255.0f blue:60.0f/255.0f alpha:1.0];
}
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"data---%@",[[NSString alloc] initWithData: data encoding:NSASCIIStringEncoding]);
[atmsData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Окончание загрузки данных
/* Парсинг */
NSError *error1 = nil;
NSDictionary *dict1 = [XMLReader dictionaryForXMLData:atmsData error:&error1];
/* Получение массива банкоматов */
arr = dict1[@"response"][@"body"][@"atms"][@"atm"];
for (int i = 0; i < arr.count; i++) {
NSDictionary *arr1 = [arr objectAtIndex:i];
NSString *name = arr1[@"name"][@"text"];
NSString *adress = arr1[@"address"][@"text"];
NSString *num = arr1[@"id"][@"text"];
NSString *state = arr1[@"status"][@"text"];
[numBankomat addObject:num];
[nameBankomat addObject:name];
[addressBankomat addObject:adress];
int status = [state intValue];
switch (status) {
case 0:
[nameImage addObject:@"signGreen@3x.png"];
break;
case 1:
[nameImage addObject:@"signYellow@3x.png"];
break;
case 2:
[nameImage addObject:@"signRed@3x.png"];
break;
}
}
[collectionView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)MySegmentControlAction:(UISegmentedControl *)segment
{
NSComparator compare = ^(NSString *obj1, NSString *obj2) {
return [obj1 compare:obj2 options:NSCaseInsensitiveSearch];
};
switch (segment.selectedSegmentIndex) {
case 1: {
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name"
ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray;
// sortedArray = [arr sortedArrayUsingDescriptors:sortDescriptors];
break;
}
default:
break;
}
}
#pragma mark Collection View Method
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [nameBankomat count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UILabel *label = (UILabel *)[cell viewWithTag:100];
label.text = [nameBankomat objectAtIndex:indexPath.row];
UILabel *labelAddress = (UILabel *)[cell viewWithTag:300];
labelAddress.text = [addressBankomat objectAtIndex:indexPath.row];
UILabel *labelNum = (UILabel *)[cell viewWithTag:500];
labelNum.text = [numBankomat objectAtIndex:indexPath.row];
UIImageView *image = (UIImageView *)[cell viewWithTag:200];
image.image = [UIImage imageNamed: [nameImage objectAtIndex:indexPath.row]];
cell.backgroundColor=[UIColor colorWithRed:67.0f/255.0f green:68.0f/255.0f blue:73.0f/255.0f alpha:1.0];
[cell.layer setBorderWidth:1.5f];
[cell.layer setBorderColor:[UIColor colorWithRed:120.0f/255.0f green:116.0f/255.0f blue:116.0f/255.0f alpha:1.0].CGColor];
[cell.layer setCornerRadius:5.0f];
return cell;
}
- (void)_collectionView:(UICollectionView *)_collectionView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Click cell collection view");
InfoBankomatView *infoController = [self.storyboard instantiateViewControllerWithIdentifier:@"InfoBankomatView"];
[self.navigationController pushViewController:infoController animated:YES];
}
/*
#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