#import
@interfaceViewController :UIViewController
@property(strong,nonatomic)UITableView*tableView;
@property(strong,nonatomic)NSArray*arr;
@property(strong,nonatomic)NSDictionary*dic;
@property(strong,nonatomic)NSMutableArray*arrMname;
@property(strong,nonatomic)NSMutableArray*arrMwerther;
@end
#import"ViewController.h"
@interfaceViewController()
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
//获取文件访问路径
NSString*path = [[NSBundlemainBundle]pathForResource:@"weather"ofType:@"json"];
//将请求解析的数据解析成data类型的对象
NSData*data = [NSDatadataWithContentsOfFile:path];
//json数据解析
__autoreleasingNSError*err;
self.dic= [NSJSONSerializationJSONObjectWithData:dataoptions:NSJSONReadingMutableLeaveserror:&err];
//用集合接收字典中第二重的keys
self.arr=self.dic[@"weatherinfo"];
// for (NSString *str in self.dic[@"weatherinfo"]) {
// NSLog(@"%@:%@",str,self.dic[@"weatherinfo"][str]);
// }
//初始化集合
self.arrMname= [NSMutableArrayarray];
self.arrMwerther= [NSMutableArrayarray];
//添加标题
self.title=@"天气预报";
//设置导航栏标题的字体大小和字体颜色
[self.navigationController.navigationBarsetTitleTextAttributes:[NSDictionarydictionaryWithObjectsAndKeys:[UIColorcolorWithRed:0.598green:0.969blue:1.000alpha:1.000],NSForegroundColorAttributeName,[UIFontfontWithName:@"Arial-Bold"size:44.0],NSFontAttributeName,nil]];
//初始化tableView
self.tableView= [[UITableViewalloc]initWithFrame:self.view.framestyle:UITableViewStylePlain];
//指定数据源代理
self.tableView.dataSource=self;
//指定代理
self.tableView.delegate=self;
//设置背景颜色
self.tableView.backgroundColor= [UIColorcolorWithRed:0.976green:1.000blue:0.896alpha:1.000];
//添加到父视图
[self.viewaddSubview:self.tableView];
}
//设置每一个分区的大小
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
return1;
}
//设置每个分区显示的行数
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
returnself.arr.count;
}
//显示每行的内容
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
NSString*iden = [NSStringstringWithFormat:@"cell%d",(int)indexPath.row];
UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:iden];
//不重用唯一标识
if(cell ==nil) {
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:iden];
}
//初始化uilabel
UILabel*lblname = [[UILabelalloc]initWithFrame:CGRectMake(0,10,207,55)];
//设置uilabel背景色
lblname.backgroundColor= [UIColorcolorWithRed:0.664green:0.981blue:1.000alpha:1.000];
//设置字体大小
lblname.font= [UIFontboldSystemFontOfSize:24];
//将字体居中
lblname.textAlignment=NSTextAlignmentCenter;
UILabel*lbldisplay = [[UILabelalloc]initWithFrame:CGRectMake(207,10,207,55)];
lbldisplay.backgroundColor= [UIColorcolorWithRed:0.730green:0.924blue:0.031alpha:1.000];
lbldisplay.font= [UIFontboldSystemFontOfSize:24];
lbldisplay.textAlignment=NSTextAlignmentCenter;
//遍历字典
for(NSString*strinself.dic[@"weatherinfo"]) {
//lblname.text = str;
//存放字典的关键字
[self.arrMnameaddObject:str];
//lbldisplay.text = self.dic[@"weatherinfo"][str];
//存放字典的值
[self.arrMwertheraddObject:self.dic[@"weatherinfo"][str]];
}
//将关键字添加到label上
lblname.text=self.arrMname[indexPath.row];
lbldisplay.text=self.arrMwerther[indexPath.row];
//contentView内容视图
//将label添加到tableview上
[cell.contentViewaddSubview:lblname];
[cell.contentViewaddSubview:lbldisplay];
returncell;
}
//显示头部标题
-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
{
return@"福州天气";
}
//设置每一行的高度
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
return60;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end