-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResults.m
185 lines (133 loc) · 4.98 KB
/
Results.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
//
// Results.m
// BNL
//
// Created by Jia Zhu on 10/09/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "Results.h"
#import "JSON.h"
#import "CustomTableCell.h"
@implementation Results
@synthesize responseString;
@synthesize resultstableView;
@synthesize resultsArray;
@synthesize addressArray;
@synthesize pubIDArray;
@synthesize total;
@synthesize navController;
@synthesize responseData;
@synthesize detailViewController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (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.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
// Do any additional setup after loading the view from its nib.
[super viewDidLoad];
NSArray* resultsData = [responseString JSONValue];
[responseString release];
total = [resultsData count];
resultsArray = [[NSMutableArray alloc] init];
addressArray = [[NSMutableArray alloc] init];
pubIDArray = [[NSMutableArray alloc] init];
for (int i = 0; i < total; i++){
NSDictionary* singleResult = [resultsData objectAtIndex:i];
NSString* name = [singleResult objectForKey:@"NAME"];
NSString* addressDetail = [singleResult objectForKey:@"ADDRESS"];
NSString* PUBID = [singleResult objectForKey:@"ID"];
[resultsArray addObject:name];
[addressArray addObject:addressDetail];
[pubIDArray addObject:PUBID];
}
}
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return total;
}
- (CustomTableCell *)tableView:(CustomTableCell *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomTableCell";
CustomTableCell *cell = (CustomTableCell *)[resultstableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:nil options:nil];
for (id currentObject in topLevelObjects){
if([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (CustomTableCell *) currentObject;
break;
}
}
}
// Configure the cell.
cell.name.text = [resultsArray objectAtIndex:indexPath.row];
cell.address.text = [addressArray objectAtIndex:indexPath.row];
cell.PUBID.text = [pubIDArray objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
CustomTableCell *cell = (CustomTableCell *)[tableView cellForRowAtIndexPath:indexPath];
self.responseData = [NSMutableData data];
NSString *url = [@"http://i-triple.com/bnl/getdata.php?id=" stringByAppendingString:cell.PUBID.text];
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[_connection release];
self.responseData = nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[_connection release];
responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
self.responseData = nil;
detailViewController = [[DetailViewController alloc] init];
detailViewController.PUBID = self.responseString;
detailViewController.navController = navController;
[navController pushViewController:detailViewController animated:YES];
// [detailViewController release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc
{
[super dealloc];
// [detailViewController release];
[resultsArray release];
[addressArray release];
[pubIDArray release];
[_connection release];
}
@end