UIPickerView是一个选择器它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活,使用也比较简单.下面做了一个关于天气预报的小Demo 用 UIPickerView 来实现.
@property (strong, nonatomic) NSDictionary *dataDic;
@property (strong, nonatomic) NSArray *cityArr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_grayView.hidden = YES;
// 1.初始化 pickerView
[self loadPickerView];
// 2.加载网络数据
[self loadDataWithRow:0];
}
- (void)loadPickerView
{
// 读取本地plist文件
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"cityCode.plist" ofType:nil];
_dataDic = [[NSDictionary alloc] initWithContentsOfFile:filePath];
// 拿到 key <城市>
_cityArr = [_dataDic allKeys];
}
- (void)loadDataWithRow:(NSInteger)row
{
_grayView.hidden = NO;
// 1.获取当前的城市下标
// NSInteger selectRow = [_pickerView selectedRowInComponent:0];
// 2.通过下标获取城市
NSString *city = _cityArr[row];
// 3.获取当前城市的ID
NSString *cityID = _dataDic[city];
// 4.通过城市ID加载数据
NSString *httpURL = [NSString stringWithFormat:@"data/sk/%@.html", cityID];
[DataService requestsDataWithURL:httpURL withBlock:^(id result) {
[self performSelector:@selector(showGrayView:) withObject:result afterDelay:.2];
}];
}
// 无敌风火轮动画
- (void)showGrayView:(id)result
{
// _grayView.hidden = NO;
// [_activiteView startAnimating];
[self performSelector:@selector(refreshUIWithDic:) withObject:result afterDelay:2];
}
//刷新UI
- (void)refreshUIWithDic:(NSDictionary *)dic {
_grayView.hidden = YES;
//取得weatherinfo的value
NSDictionary *infoDic = dic[@"weatherinfo"];
//取得城市信息
_city.text = infoDic[@"city"];
//取得城市温度
_temperature.text = infoDic[@"temp"];
//取得城市的风向
_wind.text = infoDic[@"WD"];
//取得城市的风级
_windScale.text = infoDic[@"WS"];
//取得城市的湿度
_humidity.text = infoDic[@"SD"];
//取得城市的时间
_time.text = infoDic[@"time"];
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1; // 多少列
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _cityArr.count; // 每一列多少行
}
#pragma mark - UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
// 设置没一行显示的数据
return _cityArr[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// 刷新
[self loadDataWithRow:row];
}
数据解析:
#define FIRSTURL @"http://www.weather.com.cn/"
@implementation HCDataService
+ (void)requestsDataWithURL:(NSString *)urlString withBlock:(DataBlock)block
{
// 1.创建URL
NSString *string = [NSString stringWithFormat:@"%@%@", FIRSTURL, urlString];
NSURL *url = [NSURL URLWithString:string];
// 2.创建request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
// 设置请求方式
[request setHTTPMethod:@"GET"];
// 设置超时时间
request.timeoutInterval = 10;
// 3.创建会话对象session
NSURLSession *session = [NSURLSession sharedSession];
// 4.创建会话任务task,链接服务器
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error == nil) {
dispatch_async(dispatch_get_main_queue(), ^{
id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
block(result);
});
//取得响应体
// NSString *text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// NSLog(@"%@", text);
//取得响应头
// NSHTTPURLResponse *urlResponse = (NSHTTPURLResponse *)response;
// NSDictionary *dic = urlResponse.allHeaderFields;
// NSLog(@"dic:%@",dic);
}
}];
// 开始任务
[task resume];
}
@end
滑动了UIPickView后,数据会即时刷新,显示天气情况。