-
Notifications
You must be signed in to change notification settings - Fork 0
/
YoutubeViewController.m
147 lines (89 loc) · 3.39 KB
/
YoutubeViewController.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
//
// YoutubeViewController.m
// marspaper
//
// Created by kuzey on 26.06.2014.
// Copyright (c) 2014 kuzey. All rights reserved.
//
#import "YoutubeViewController.h"
#import "VideoModel.h"
#import "HUD.h"
@interface YoutubeViewController (){
IBOutlet UITableView * table;
NSArray *items;
}
@end
@implementation YoutubeViewController
-(void)viewDidAppear:(BOOL)animated{
}
- (void)viewDidLoad
{
[super viewDidLoad];
[table setBackgroundView:
[[UIImageView alloc] initWithImage:
[UIImage imageNamed:@"marssun.png"]]];
[table setBackgroundColor:
[UIColor colorWithPatternImage:
[UIImage imageNamed:@"marssun.png"]]];
self.title=@"NASA";
[HUD showUIBlockingIndicatorWithText:@"Loading"];
dispatch_async (dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData * ytData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://gdata.youtube.com/feeds/api/videos?q=nasa&max-results=50&alt=json"]];
NSDictionary * json = [NSJSONSerialization JSONObjectWithData:ytData options:kNilOptions error:nil];
dispatch_async(dispatch_get_main_queue() ,^ {
items= [VideoModel arrayOfModelsFromDictionaries:json[@"feed"][@"entry"]];
[HUD hideUIBlockingIndicator];
if (items) {
[table reloadData];
}else {
[HUD showAlertWithTitle:@"Error" text:@"Sorry, invalid data"];
}
});
});
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - table methods
-(NSInteger)numberOfSectionInTableView:(UITableView*)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
return items.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
}
VideoModel* videio = items[indexPath.row];
cell.textLabel.text=videio.title.$t;
return cell;
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[table deselectRowAtIndexPath:indexPath animated:YES];
VideoModel * video = items[indexPath.row];
[[UIApplication sharedApplication] openURL:video.link.href];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end