-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestViewController.m
181 lines (121 loc) · 5.36 KB
/
TestViewController.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
//
// SecondViewController.m
// Chatter
//
// Created by Richard Lung on 2/19/12.
// Copyright 2012 __MyCompanyName__. All rights reserved.
//
#import "SecondViewController.h"
#import "ForFun.h"
#import "ASIFormDataRequest.h"
@implementation SecondViewController
@synthesize myTableView;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
titleArray = [[NSMutableArray alloc] init ];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
// Dummy data that will be retrieved from the webserver
NSString *jsonString = @"[{\"postId\": 1, \"title\": \"think i found something cool\", \"timestamp\": 318791347981, \"numUpvotes\": 23, \"numDownvotes\": 4, \"numComments\": 49}, {\"postId\": 2, \"title\":\"check this out UCLA!\", \"timestamp\": 318791323411, \"numUpvotes\": 19, \"numDownvotes\": 2, \"numComments\": 31}]";
// Parse the JSON String into an NSDictionary object
/* NSDictionary *deserializedData = [jsonString objectFromJSONString];
// Log the results
NSLog(@"%@", [deserializedData description]);
// Iterate through each post in the array
for (NSDictionary * dataDict in deserializedData) {
// Extract the Post ID # from this post
NSString * postTitle = [dataDict objectForKey:@"title"];
NSLog(@"%@", postTitle);
// Extract ..... everything else
}
// For each post in the array, add the post to the UI view (including the title, num upvotes, etc.)
// Refresh the view? (don't know if this is necessary)
*/
[super viewDidLoad];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
lat = @"";
lon = @"";
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocationCoordinate2D loc = [newLocation coordinate];
lat = [[NSString stringWithFormat: @"%f", loc.latitude] retain];
lon = [[NSString stringWithFormat: @"%f", loc.longitude] retain];
[locationManager stopUpdatingLocation];
ASIFormDataRequest *request;
NSLog(@"%@", lat);
NSString * theStringURL = [NSString stringWithFormat:@"%@%@%@%@", @"http://www.williamliwu.com/chatter/getNearbyThreads.php?lat=", lat, @"&lng=", lon];
NSLog(@"HAHA%@", theStringURL);
request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:theStringURL]];
[request setCompletionBlock:^{
NSDictionary *deserializedData = [request.responseString objectFromJSONString];
//NSLog(@"%@", request.responseString);
for (NSDictionary * dataDict in deserializedData) {
// Extract the Post ID # from this post
// NSString * postTitle = [dataDict objectForKey:@"UPVOTES"];
//NSLog(@"%@", postTitle);
NSString *testMe = [dataDict objectForKey:@"TITLE"];
// NSLog(@"%@", testMe);
[titleArray addObject:testMe];
[self.myTableView reloadData];
}
// NSLog([titleArray objectAtIndex:0]);
}];
[request setFailedBlock:^{
NSLog(@"%@", request.error);
}];
[request startAsynchronous];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [NSString stringWithFormat:@"%@",[titleArray objectAtIndex:indexPath.row]];
cell.detailTextLabel.text = @"alexw";
NSLog([titleArray objectAtIndex:indexPath.row]);
//cell.value
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ForFun *promptController = [[ForFun alloc] initWithNibName:@"ForFun" bundle:nil];
NSLog(@"YEAH");
//[self.navigationController pushViewController:promptController animated:YES];
//self.view = promptController.view;
[self presentModalViewController:promptController animated:YES];
[promptController release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//NSLog([[titleArray count]])
return [titleArray count];
}
- (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