天天看點

iOS--JSON解析後如何擷取資料,并且展示到相應cell上

iOS--JSON解析後如何擷取資料,并且展示到相應cell上

首先建立一個singleview工程,并在故事版中添加UITableView,連好資料源和代理。

在.h檔案中添加資料源和代理方法,并且聲明一個UITableView的變量,代碼如下。

<a href="http://my.oschina.net/gexun/blog/267667#" target="_blank">?</a>

1

2

3

4

5

6

7

<code>#import &lt;UIKit/UIKit.h&gt;</code>

<code>@interface ViewController : UIViewController&lt;UITableViewDataSource,UITableViewDelegate&gt;</code>

<code>@property (weak, nonatomic) IBOutlet UITableView *tableview;</code>

<code>@end</code>

剩下的代碼比較簡單,我就直接貼代碼了,已經在裡面詳細注釋了。

.m檔案如下

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

<code>#import "ViewController.h"</code>

<code>@interface ViewController ()</code>

<code>{</code>

<code>    </code><code>NSMutableArray *_listarr;  </code><code>//定義一個存放資料的容器。</code>

<code>}</code>

<code>@implementation ViewController</code>

<code>- (</code><code>void</code><code>)viewDidLoad</code>

<code>    </code><code>[super viewDidLoad];</code>

<code>    </code> 

<code>    </code><code>//ios7新特性,這個請自行百度。</code>

<code>    </code><code>[self.tableview registerClass:[UITableViewCell </code><code>class</code><code>] forCellReuseIdentifier:@</code><code>"Cell"</code><code>];</code>

<code>    </code><code>NSError *error;</code>

<code>    </code><code>//加載一個NSURL對象</code>

<code>    </code><code>//将請求的url資料放到NSData對象中</code>

<code>    </code><code>NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];</code>

<code>    </code><code>//IOS5自帶解析類NSJSONSerialization從response中解析出資料放到字典中</code>

<code>    </code><code>NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&amp;error];</code>

<code>    </code><code>//擷取數組list的内容</code>

<code>    </code><code>NSArray *list = [dic objectForKey:@</code><code>"list"</code><code>];</code>

<code>    </code><code>NSLog(@</code><code>"list數組的内容為--》%@"</code><code>, list );</code>

<code>    </code><code>//初始化成員變量</code>

<code>    </code><code>_listarr = [[NSMutableArray alloc]init];</code>

<code>    </code><code>//周遊數組list裡的内容</code>

<code>    </code><code>for</code> <code>(</code><code>int</code> <code>i = 0; i&lt;[list count]; i++) {</code>

<code>        </code> 

<code>        </code><code>//按數組中的索引取出對應的字典</code>

<code>        </code><code>NSDictionary *listdic = [list objectAtIndex:i];</code>

<code>        </code><code>//通過字典中的key取出對應value,并且強制轉化為NSString類型</code>

<code>         </code><code>NSString *teamname = (NSString *)[listdic objectForKey:@</code><code>"name_j"</code><code>];</code>

<code>        </code><code>//将擷取的value值放到數組容器中</code>

<code>        </code><code>[_listarr addObject:teamname];</code>

<code>        </code><code>NSLog(@</code><code>"name内容為--》%@"</code><code>, teamname );</code>

<code>    </code><code>}</code>

<code>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section</code>

<code>    </code><code>//必須傳回與資料容器一樣的數量,否則會報資料越界錯</code>

<code>    </code><code>return</code> <code>[_listarr count];</code>

<code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath</code>

<code>    </code><code>static</code> <code>NSString *CellIdentifier = @</code><code>"Cell"</code><code>;</code>

<code>    </code><code>UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];</code>

<code>    </code><code>//将要顯示的資料指派到cell上</code>

<code>    </code><code>cell.textLabel.text = [_listarr objectAtIndex:indexPath.row];</code>

<code>    </code><code>return</code> <code>cell;</code>

本文轉自 卓行天下  51CTO部落格,原文連結:http://blog.51cto.com/9951038/1747480,如需轉載請自行聯系原作者

繼續閱讀