清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
@interface ViewController (){
NSMutableArray *arrayData; //保存城市id
NSMutableArray *arrayCityName; //pick中的城市名字
UIPickerView *picker; //下方滚动视图
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_wu.hidden = YES;
//city
arrayData = [[NSMutableArray alloc] init];
arrayCityName = [[NSMutableArray alloc] init];
NSString *path = [[NSBundle mainBundle] pathForResource:@"cityCode" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:path];
const NSArray *array = [dictionary allKeys];
for (int i = 0;i < array.count; i++) {
NSString *idd = [dictionary objectForKey:[array objectAtIndex:i]];
[arrayData addObject:idd];
[arrayCityName addObject:[array objectAtIndex:i]];
}
//pick
picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 400, self.view.bounds.size.width, self.view.bounds.size.height - 400)];
picker.delegate = self;
picker.dataSource = self;
[self.view addSubview:picker];
picker.showsSelectionIndicator = YES;
//request
blockJson = ^(NSInteger day,NSString *path){
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://m.weather.com.cn/atad/%@.html",path]];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"%@",response);
if (data == nil) {
NSLog(@"error: %@",error);
}else{
NSError *jsonError;
NSDictionary *jsonDiction = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];
if (jsonDiction == nil) {
NSLog(@"error %@",jsonError);
_wu.hidden = NO;
}else {
//回到主线程更改UI
NSDictionary *dictionary = [jsonDiction objectForKey:@"weatherinfo"];
dispatch_async(dispatch_get_main_queue(), ^{
_city.text = [dictionary objectForKey:@"city"];
_wenDu.text = [dictionary objectForKey:[NSString stringWithFormat:@"temp%ld",(day + 1)]];
_fengXiang.text = [dictionary objectForKey:[NSString stringWithFormat:@"fx%ld",(day + 1)]];
_fengJi.text = [dictionary objectForKey:[NSString stringWithFormat:@"fl%ld",(day + 1)]];
_tianQi.text = [dictionary objectForKey:[NSString stringWithFormat:@"weather%ld",(day + 1)]];
_shiJian.text = [dictionary objectForKey:@"date_y"];
_jianYi.text = [dictionary objectForKey:@"index_d"];
});
}
}
}];
[task resume];
};
}
//指定列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
//2.指定每列中的行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == 1) {
return arrayData.count;
}else{
return 6;
}
}
//自定义pick行视图
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view {
if(view == nil){
UILabel *lable = [[UILabel alloc] init];
lable.backgroundColor = [UIColor orangeColor];
if (component == 1) {
lable.text = [arrayCityName objectAtIndex:row];
}else{
lable.text = [NSString stringWithFormat:@"今后第: %ld 天",row];
}
lable.textAlignment = NSTextAlignmentCenter;
view = (UIView *)lable;
}
return view;
}
//选中行,调用的协议方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSString *path = [arrayData objectAtIndex:[pickerView selectedRowInComponent:1]];
NSInteger day = [pickerView selectedRowInComponent:0];
NSLog(@"---------------zuo %ld hang %ld",[pickerView selectedRowInComponent:0],[pickerView selectedRowInComponent:1]);
blockJson(day,path);
_wu.hidden = YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)pinLa:(id)sender {
[picker reloadAllComponents];
_wu.hidden = YES;
}
//点击查询按钮显示第一个城市信息
- (IBAction)chaXun:(id)sender {
NSString *path = [arrayData objectAtIndex:0];
blockJson(0,path);
//UIPickerView显示指定行
[picker selectRow:0 inComponent:0 animated:YES];
[picker selectRow:0 inComponent:1 animated:YES];
_wu.hidden = YES;
}
@end