29,028
社区成员
发帖
与我相关
我的任务
分享
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Store incoming data into a string
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// Create a dictionary from the JSON string
NSDictionary *results = [jsonString JSONValue];
// Build an array from the dictionary for easy access to each entry
NSArray *photos = [[results objectForKey:@"photos"] objectForKey:@"photo"];
// Loop through each entry in the dictionary...
for (NSDictionary *photo in photos)
{
// Get title of the image
NSString *title = [photo objectForKey:@"title"];
// Save the title to the photo titles array
[photoTitles addObject:(title.length > 0 ? title : @"Untitled")];
NSString *photoURLString = [NSString stringWithFormat:@"http://farm%@.static.flickr.com/%@/%@_%@_s.jpg", [photo objectForKey:@"farm"], [photo objectForKey:@"server"], [photo objectForKey:@"id"], [photo objectForKey:@"secret"]];
[photoSmallImageData addObject:[NSData dataWithContentsOfURL:[NSURL URLWithString:photoURLString]]];
photoURLString = [NSString stringWithFormat:@"http://farm%@.static.flickr.com/%@/%@_%@_m.jpg", [photo objectForKey:@"farm"], [photo objectForKey:@"server"], [photo objectForKey:@"id"], [photo objectForKey:@"secret"]];
[photoURLsLargeImage addObject:[NSURL URLWithString:photoURLString]];
}
// Update the table with data
[theTableView reloadData];
// Stop the activity indicator
[activityIndicator stopAnimating];
[jsonString release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BigPicViewController *bpvc = [[BigPicViewController alloc] init];
bpvc.array = [photoURLsLargeImage objectAtIndex:indexPath.row];
[self.navigationController pushViewController:bpvc animated:YES];
[bpvc release];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cachedCell"];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"cachedCell"] autorelease];
#if __IPHONE_3_0
cell.textLabel.text = [photoTitles objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:13.0];
#else
cell.text = [photoTitles objectAtIndex:indexPath.row];
cell.font = [UIFont systemFontOfSize:13.0];
#endif
NSData *imageData = [photoSmallImageData objectAtIndex:indexPath.row];
#if __IPHONE_3_0
cell.imageView.image = [UIImage imageWithData:imageData];
#else
cell.image = [UIImage imageWithData:imageData];
#endif
return cell;
}
[photoSmallImageData addObject:[NSData dataWithContentsOfURL:[NSURL URLWithString:photoURLString]]];//这个其中的dataWithContentsOfURL才是你需要做异步处理的地方吧,下载需要时间的啊
[[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL: url] delegate:self];