-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAM.m
367 lines (248 loc) · 11.1 KB
/
AM.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//
// AM.m
// Esqueleto DCU
//
// Created by julio.guzman on 18/11/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
//Vista Tipo Lista
/*
Descripción/Datos que utiliza
Entrada:
Se alimenta de los datos obtenidos por un servicio web
Acción:
Construye una lista con los autores ordenados alfabeticamente y con separadores en cada letra.
Salida:
Al dar click en un elemento nos lleva a la descripción del mismo
Al dar click en los botones inferiores nos lleva a la vista correcpondiente.
*/
#import "AM.h"
#import "SBJSON.h"
#import "LA.h"
#define LLAVE @"titulo"
@implementation AM
@synthesize songs, sections;
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
if (searching) {
return 1;
}
return [[self.sections allKeys] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (searching) {
return nil;
}
return [[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (searching) {
return [displayItems count];
}
return [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] count];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
if (searching) {
return nil;
}
return [[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleGray;
if (searching) {
cell.textLabel.text = [[displayItems objectAtIndex:indexPath.row] objectForKey:LLAVE];
//cell.detailTextLabel.text = [[displayItems objectAtIndex:indexPath.row] objectForKey:@"id"];
return cell;
}
NSDictionary *song = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.text = [song objectForKey:LLAVE];
//cell.detailTextLabel.text = [song objectForKey:@"id"];
return cell;
}
- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if ([searchText length]==0) {
[displayItems removeAllObjects];
[displayItems addObjectsFromArray:totalItemsOrdenados];
} else {
[displayItems removeAllObjects];
//for (int i=0; i<[totalItemsOrdenados count]; i++) {
int i=0;
for(NSString *string in totalItemsOrdenadosX){
NSRange r = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (r.location != NSNotFound) {
[displayItems addObject:[totalItemsOrdenados objectAtIndex:i]];
}
i++;
}
//}
}
searching=YES;
[tableView reloadData];
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)aSearchBar{
searching=YES;
[searchBar resignFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)aSearchBar{
searchBar.text =@"";
searching=NO;
[searchBar resignFirstResponder];
[tableView reloadData];
}
- (IBAction) goBack {
[self dismissModalViewControllerAnimated:YES];
}
- (NSString *) documentsPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return documentsDir;
}
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"fondo.png"]];
[activityIndicator startAnimating];
[NSThread detachNewThreadSelector:@selector(viewDidThread) toTarget:self withObject:nil];
}
- (void) viewDidThread {
//////////////////////////////////////
@try {
SBJSON *parser = [[SBJSON alloc] init];
NSString *string = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.unam360.com/dcu/service/filtro?tipo=autores"] encoding:NSISOLatin1StringEncoding error:nil];
NSMutableDictionary *arra = [parser objectWithString:string error:nil];
//[jsonDictionary writeToFile:filename atomically:YES];
NSMutableDictionary *temp = (NSMutableDictionary *) [arra objectForKey:@"datos"];
self.songs = [[NSMutableArray alloc] init];
for(NSDictionary *x in temp){
[self.songs addObject:[x objectForKey:@"datos"]];
}
/*for(int i=0;i<[self.songs count];i++){
NSLog(@"%@",[self.songs objectAtIndex:i]);
}*/
///////////////////////////////////////////////////////
self.sections = [[NSMutableDictionary alloc] init];
BOOL found;
// Loop through the books and create our keys
for (int i=0; i<[self.songs count]; i++) {
for (NSDictionary *song in [self.songs objectAtIndex:i])
{
NSString *c = [[song objectForKey:LLAVE] substringToIndex:1];
found = NO;
for (NSString *str in [self.sections allKeys])
{
if ([str isEqualToString:c])
{
found = YES;
}
}
if (!found)
{
[self.sections setValue:[[NSMutableArray alloc] init] forKey:c];
}
}
for (NSDictionary *song in [self.songs objectAtIndex:i])
{
[[self.sections objectForKey:[[song objectForKey:LLAVE] substringToIndex:1]] addObject:song];
}
}
// Loop again and sort the books into their respective keys
// Sort each section array
for (NSString *key in [self.sections allKeys])
{
[[self.sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:LLAVE ascending:YES]]];
}
searching=NO;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHidden:) name:UIKeyboardWillHideNotification object:nil];
totalItemsOrdenados = [[NSMutableArray alloc] init];
totalItemsOrdenadosX = [[NSMutableArray alloc] init];
for (int i=0; i<[self.songs count]; i++) {
for (NSDictionary *song in [self.songs objectAtIndex:i]) {
NSString *s = [song objectForKey:LLAVE];
[totalItemsOrdenados addObject:song];
[totalItemsOrdenadosX addObject:s];
}
}
//NSArray *sortedArray = [totalItemsOrdenados sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
displayItems = [[NSMutableArray alloc] initWithArray:totalItemsOrdenados];
}
@catch (NSException *exception) {
[self reachability];
}
@finally {
if (![self internetreachability]) {
[self reachability];
}
}
[tableView reloadData];
[activityIndicator setHidesWhenStopped:YES];
[activityIndicator stopAnimating];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
LA *nextPage;
if (searching) {
nextPage = [[LA alloc] initWithNibName:@"LA" bundle:nil idNumber:[NSString stringWithFormat:@"%@", [[displayItems objectAtIndex:indexPath.row] objectForKey:@"id"]] categoryName:[NSString stringWithFormat:@"%@", [[displayItems objectAtIndex:indexPath.row] objectForKey:LLAVE]]];
nextPage.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.view addSubview:nextPage.view];
[self presentModalViewController:nextPage animated:YES];
} else {
NSDictionary *song = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
nextPage = [[LA alloc] initWithNibName:@"LA" bundle:nil idNumber:[NSString stringWithFormat:@"%@", [song objectForKey:@"id"]] categoryName:[NSString stringWithFormat:@"%@", [song objectForKey:LLAVE]] ];
nextPage.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.view addSubview:nextPage.view];
[self presentModalViewController:nextPage animated:YES];
}
}
- (void)keyboardShown:(NSNotification *)note {
searching = YES;
CGRect keyboardFrame;
[[[note userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
CGRect tableViewFrame = tableView.frame;
CGRect searchBarFrame = searchBar.frame;
tableViewFrame.size.height = tableViewFrame.size.height-keyboardFrame.size.height+searchBarFrame.size.height;
[tableView setFrame:tableViewFrame];
//searching = YES;
[tableView reloadData];
}
- (void)keyboardHidden:(NSNotification *)note {
[tableView setFrame:self.view.bounds];
CGRect tableViewFrame = tableView.frame;
tableViewFrame.size.height = tableViewFrame.size.height -88-44;
tableViewFrame.origin.y=88;
[tableView setFrame:tableViewFrame];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end