forked from jsonmodel/jsonmodel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubViewController.m
More file actions
95 lines (76 loc) · 2.57 KB
/
GitHubViewController.m
File metadata and controls
95 lines (76 loc) · 2.57 KB
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
//
// GitHubViewController.m
// JSONModelDemo
//
// Created by Marin Todorov on 02/12/2012.
// Copyright (c) 2012 Underplot ltd. All rights reserved.
//
#import "GitHubViewController.h"
#import "GitHubUserModel.h"
#import "HUD.h"
@interface GitHubViewController ()
{
GitHubUserModel* user;
NSArray* items;
}
@end
@implementation GitHubViewController
-(void)viewDidAppear:(BOOL)animated
{
self.title = @"GitHub.com user lookup";
[HUD showUIBlockingIndicatorWithText:@"Fetching JSON"];
//1
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//code executed in the background
//2
NSData* ghData = [NSData dataWithContentsOfURL:
[NSURL URLWithString:@"https://api.github.com/users/icanzilb"]
];
//3
NSDictionary* json = nil;
if (ghData) {
json = [NSJSONSerialization
JSONObjectWithData:ghData
options:kNilOptions
error:nil];
}
//4
dispatch_async(dispatch_get_main_queue(), ^{
//code executed on the main queue
//5
user = [[GitHubUserModel alloc] initWithDictionary:json error:NULL];
items = @[user.login, user.html_url, user.company, user.name, user.blog];
[self.tableView reloadData];
[HUD hideUIBlockingIndicator];
});
});
}
#pragma mark - table methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return items.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KivaCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"KivaCell"];
}
cell.textLabel.text = [items[indexPath.row] description];
if ([items[indexPath.row] isKindOfClass:[NSURL class]]) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([items[indexPath.row] isKindOfClass:[NSURL class]]) {
[[UIApplication sharedApplication] openURL:items[indexPath.row]];
}
}
@end