安哥网络 发表于 2013-6-27 13:58:53

UITableView 分页显示,数据源远程数据

在这篇里,我们要学习的目标:

[*]1. 解析远程的JSON数据
[*]2. 分页显示数据
[*]3. 学习Category的使用
概述

    ios5.0开始支持JSON,所以不用第三方的解析了,主要用于这三个对象:NSDictionary、NSData、NSJSONSerializatio
今天我们做一个天气预报数据的展示,通过国家气象局提供的天气预报接口来获取数据。注:以实现功能为主,不做过多的界面上展示
接口地址:


[*]http://www.weather.com.cn/data/sk/101010100.html
[*]http://www.weather.com.cn/data/cityinfo/101010100.html
[*]http://m.weather.com.cn/data/101010100.html
    以第三个地址显示内容最全,我们就以这个地获取数据,返回的数据格式如下:   

http://www.189works.com/data/attachment/portal/et2/201209/ET39916080900141.gifView Code 1 {
2   "weatherinfo":{
3         "city":"北京",
4         "city_en":"beijing",
5         "date_y":"2012年9月5日",
6         "date":"",
7         "week":"星期三",
8         "fchh":"11",
9         "cityid":"101010100",
10         "temp1":"27℃~17℃",
11         "temp2":"24℃~17℃",
12         "temp3":"25℃~17℃",
13         "temp4":"27℃~16℃",
14         "temp5":"28℃~17℃",
15         "temp6":"26℃~17℃",
16         "tempF1":"80.6℉~62.6℉",
17         "tempF2":"75.2℉~62.6℉",
18         "tempF3":"77℉~62.6℉",
19         "tempF4":"80.6℉~60.8℉",
20         "tempF5":"82.4℉~62.6℉",
21         "tempF6":"78.8℉~62.6℉",
22         "weather1":"晴转阴",
23         "weather2":"阵雨",
24         "weather3":"阵雨转多云",
25         "weather4":"多云转晴",
26         "weather5":"晴",
27         "weather6":"多云转阵雨",
28         "img1":"0",
29         "img2":"2",
30         "img3":"3",
31         "img4":"99",
32         "img5":"3",
33         "img6":"1",
34         "img7":"1",
35         "img8":"0",
36         "img9":"0",
37         "img10":"99",
38         "img11":"1",
39         "img12":"3",
40         "img_single":"0",
41         "img_title1":"晴",
42         "img_title2":"阴",
43         "img_title3":"阵雨",
44         "img_title4":"阵雨",
45         "img_title5":"阵雨",
46         "img_title6":"多云",
47         "img_title7":"多云",
48         "img_title8":"晴",
49         "img_title9":"晴",
50         "img_title10":"晴",
51         "img_title11":"多云",
52         "img_title12":"阵雨",
53         "img_title_single":"晴",
54         "wind1":"微风",
55         "wind2":"微风",
56         "wind3":"微风",
57         "wind4":"微风",
58         "wind5":"微风",
59         "wind6":"微风",
60         "fx1":"微风",
61         "fx2":"微风",
62         "fl1":"小于3级",
63         "fl2":"小于3级",
64         "fl3":"小于3级",
65         "fl4":"小于3级",
66         "fl5":"小于3级",
67         "fl6":"小于3级",
68         "index":"暖",
69         "index_d":"较凉爽,建议着长袖衬裤等春秋过渡装。体弱者宜着长袖衬衫和马甲。但昼夜温差较大,请适当增减衣服。",
70         "index48":"暖",
71         "index48_d":"较凉爽,建议着长袖衬衫加单裤等春秋过渡装。年老体弱者宜着针织长袖衬衫、马甲和长裤。",
72         "index_uv":"中等",
73         "index48_uv":"弱",
74         "index_xc":"不宜",
75         "index_tr":"适宜",
76         "index_co":"较舒适",
77         "st1":"26",
78         "st2":"17",
79         "st3":"22",
80         "st4":"16",
81         "st5":"23",
82         "st6":"17",
83         "index_cl":"适宜",
84         "index_ls":"适宜",
85         "index_ag":"易发"
86   }
87 }




    Category 是Objective-C 里面最常用到的功能之一。简单的讲Category可以为已经存在的类增加方法,而不需要增加一个子类。而且,我们可以在不知道某个类内部实现的情况下,为该类增加方法。如果我们想增加某个框架(framework)中的类的方法,Category就非常有效。比如,今天的内容中,就想在NSDictionary上增加一个方法来直接处理URL远程数据,后面有详细代码。定义格式如下: #import "类名.h"
2   @interface 类名(类别名)
3         //新方法的声明
4   @end

具体操作,如下步骤:
1. 创建项目,项目名称:listCityWeather
http://www.189works.com/data/attachment/portal/et2/201209/ET39916080900143.jpg

2. 添加一个Category类,扩展NSDictionary,主要方便于处理远程的json数据
http://www.189works.com/data/attachment/portal/et2/201209/ET39916080900144.jpg
http://www.189works.com/data/attachment/portal/et2/201209/ET39916080900145.jpg
2.1 修改NSDictionary+Json.h文件1 //
2 //NSDictionary+Json.h
3 //listCityWeather
4 //
5 //Created by tony on 12-9-5.
6 //Copyright (c) 2012年 chinapcc.com. All rights reserved.
7 //
8
9 #import
10
11 @interface NSDictionary (Json)
12
13
14 // 直把远程的地址上Json数据转,换成Dictionary对象
15 +(NSDictionary*)dictionaryWithContentsOfURLString:(NSString*)urlAddress;
16
17 // 把当前的Dictionary对象,转成Json对象
18 -(NSData*)toJSON;
19
20 @end



2.2 修改NSDictionary+Json.m文件
1 //
2 //NSDictionary+Json.m
3 //listCityWeather
4 //
5 //Created by tony on 12-9-5.
6 //Copyright (c) 2012年 chinapcc.com. All rights reserved.
7 //
8
9 #import "NSDictionary+Json.h"
10
11 @implementation NSDictionary (Json)
12
13 // 直把远程的地址上Json数据转,换成Dictionary对象
14 +(NSDictionary*)dictionaryWithContentsOfURLString:(NSString*)urlAddress
15 {
16   // 请求远程数据,存放到NSData对象中
17   NSData* data =];
18   
19   // 定义一个错误信息的对象
20   __autoreleasing NSError *error =nil;
21   
22   // 序列化字符串
23   id result =[NSJSONSerialization JSONObjectWithData:data
24                                                options:kNilOptions error:&error];
25   if(error !=nil)
26         return nil;
27   
28   return result;
29 }
30
31 // 把当前的Dictionary对象,转成Json对象
32 -(NSData*)toJSON{
33   NSError *error =nil;
34   // 把当前的Dictionary对象转换成字符串
35   id result =[NSJSONSerialization dataWithJSONObject:self
36                                                options:kNilOptions error:&error];
37   if(error !=nil)
38         return nil;
39   
40   return result;
41 }
42
43 @end



2.3 调用方法如下:
1 NSDictionary *city =;
2
3 NSLog(@" city: %@",[ valueForKey:@"city"] );
4
5 /////////////////////////////////////////////////
6
7 NSDictionary *myInfo =;
8 NSData *json =;
9      
10 NSLog(@" json: %@",[initWithData:json encoding:NSUTF8StringEncoding]);3. 添加plist数据, 地区资料,我就懒得做服务器端程序,这里就用一个plist数据来展示中国有天气的城市列表
北京
      101010100
      朝阳
      101071201

本文摘自:http://www.189works.com/article-94934-1.html



页: [1]
查看完整版本: UITableView 分页显示,数据源远程数据