您的位置:首页 > 产品设计 > UI/UE

iOS学习之驾照题库(练习页面跳转,传值,UITableView,原生json解析)

2015-12-16 17:45 597 查看
仅供自己学习参考笔记

效果图:



新建项目JSONParseDemo

在Main.storyboard拉取UItableView控件到View上(一定不要在此刻关联协议,协议必须在数据从网络端获取到以后再绑定协议),如图:





新建TableViewCell类并创建xib文件



其次,根据数据请求到的json字符串中的字段新建Exams和Details类,用于解析

->Exams.h

#import <Foundation/Foundation.h>
@interface Exams : NSObject
@property(copy,nonatomic) NSString* error_code;
@property(copy,nonatomic) NSString* reason;
@property(copy,nonatomic)NSArray* result;
@end

->Exam.m#import "Exams.h"

@implementation Exams
@synthesize error_code;
@synthesize reason;
@synthesize result;
@end

->Details.h
#import <Foundation/Foundation.h>

@interface Details : NSObject
@property (copy,nonatomic) NSString* cid;
@property(copy,nonatomic) NSString* question;
@property(copy,nonatomic) NSString* anwser;
@property(copy,nonatomic) NSString* item1;
@property(copy,nonatomic) NSString* item2;
@property(copy,nonatomic) NSString* item3;
@property(copy,nonatomic) NSString* item4;
@property(copy,nonatomic) NSString* explains;
@property(copy,nonatomic) NSString* url;
@end
->Details.m

#import "Details.h"

@implementation Details
@synthesize cid;
@synthesize question;
@synthesize anwser;
@synthesize item1;
@synthesize item2;
@synthesize item3;
@synthesize item4;
@synthesize explains;
@synthesize url;
@end

ViewController.m代码

//
// ViewController.m
// JSONParse
//
// Created by 安前松 on 15/12/16.
// Copyright © 2015年 安前松. All rights reserved.
//

#import "ViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
static NSString *urlString=@"http://apis.baidu.com/bbtapi/jztk/jztk_query?subject=1&model=c1&testType=rand";
static NSString* apikey=@"751416ad67dd313690c2caa7633f992b";
NSURL *url=[NSURL URLWithString:urlString];
[self request:url withHttpArg:apikey];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(void) request:(NSURL*) requestUrl withHttpArg:(NSString*) apikey{
NSMutableURLRequest * request=[[NSMutableURLRequest alloc] initWithURL:requestUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
[request setHTTPMethod:@"GET"];
[request addValue:apikey forHTTPHeaderField:@"apikey"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response,NSData *data,NSError *error){
if(error){
NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code);
}else{
NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"HttpResponseCode:%ld", responseCode);
// NSLog(@"HttpResponseBody:%@",responseString);
[self parse:data];

} }];

}

-(void)parse:(NSData*)data{
NSError *error=nil;
id jsonObject=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
if([jsonObject isKindOfClass:[NSDictionary class]]){

NSDictionary *dictionary=(NSDictionary*)jsonObject;
Exams * exam=[Exams new];
NSString *error_code=[dictionary objectForKey:@"error_code"];
NSString *reason=[dictionary objectForKey:@"reason"];
exam.error_code=error_code;
exam.reason=reason;
NSArray *result=[dictionary objectForKey:@"result"];
NSMutableArray *details=[[NSMutableArray alloc] init];
for(int i=0;i<[result count];i++){
Details *detail=[Details new];
NSDictionary *d=(NSDictionary*)result[i];
NSString* cid=[d objectForKey:@"id"];
NSString * question=[d objectForKey:@"question"];
NSString * anwser=[d objectForKey:@"answer"];
NSString* item1=[d objectForKey:@"item1"];
NSString* item2=[d objectForKey:@"item2"];
NSString* item3=[d objectForKey:@"item3"];
NSString* item4=[d objectForKey:@"item4"];
NSString* explains=[d objectForKey:@"explains"];
NSString* url=[d objectForKey:@"url"];
detail.cid=cid;
detail.question=question;
detail.anwser=anwser;
detail.item1=item1;
detail.item2=item2;
detail.item3=item3;
detail.item4=item4;
detail.explains=explains;
detail.url=url;
[details insertObject:detail atIndex:i];
detail=nil;
}
self.datas=details;
exam.result=details;
[self initView:exam];
}
}

-(void) initView:(Exams*) data{
self.table.delegate=self;
self.table.dataSource=self;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.datas count];

}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
TableViewCell *cell=[self.table dequeueReusableCellWithIdentifier:@"cell"];
if(cell==nil){
cell=[[[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil] lastObject];
}
Details *d=self.datas[indexPath.row];
NSString *str=d.cid;
NSString* text=[[NSString alloc] initWithString:[NSString stringWithFormat:@"题目%@",str]];
cell.label.text=text;
return cell;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetailView *detailView=[[DetailView alloc] initWithNibName:@"DetailView" bundle:nil];
detailView.detail=self.datas[indexPath.row];
[detailView setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:detailView animated:YES];
}

@end


详情页面:
新建DetailView并创建xib



->DetailView.h

#import <UIKit/UIKit.h>
#import "Details.h"
@interface DetailView : UIViewController

@property (strong, nonatomic) IBOutlet UITextView *tv_question;
@property (strong, nonatomic) IBOutlet UITextView *item1;
@property (strong, nonatomic) IBOutlet UITextView *item2;
@property (strong, nonatomic) IBOutlet UITextView *item3;
@property (strong, nonatomic) IBOutlet UITextView *item4;
- (IBAction)back:(id)sender;

@property (strong, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)button:(id)sender;
@property (strong, nonatomic) IBOutlet UITextView *tv_anwser;

@property(strong,nonatomic) Details* detail;
@end

->DetailView.m
#import "DetailView.h"

@interface DetailView ()

@end

@implementation DetailView

- (void)viewDidLoad {
[super viewDidLoad];
self.tv_question.text=self.detail.question;
NSString *A=[[NSString alloc] initWithString:[NSString stringWithFormat:@"A:%@",self.detail.item1]];
NSString *B=[[NSString alloc] initWithString:[NSString stringWithFormat:@"B:%@",self.detail.item2]];
NSString *C=[[NSString alloc] initWithString:[NSString stringWithFormat:@"C:%@",self.detail.item3]];
NSString *D=[[NSString alloc] initWithString:[NSString stringWithFormat:@"D:%@",self.detail.item4]];
self.item1.text=A;
self.item2.text=B;
self.item3.text=C;
self.item4.text=D;
NSURL *picUrl=[NSURL URLWithString:self.detail.url];
if(picUrl!=nil){
NSData *data=[NSData dataWithContentsOfURL:picUrl];
UIImage *image=[UIImage imageWithData:data];
self.imageView.image=image;
}
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)button:(id)sender {
NSString *s=self.detail.anwser;
if([s isEqualToString:@"1"]){
s=@"A";
}else if([s isEqualToString:@"2"]){
s=@"B";
}else if([s isEqualToString:@"3"]){
s=@"C";
}else if([ s isEqualToString:@"4"]){
s=@"D";
}
NSString *anwser=[[NSString alloc] initWithString:[NSString stringWithFormat:@"答案:%@",s]];
self.tv_anwser.text=anwser;
}
- (IBAction)back:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: