2014-02-26 29 views
0

我目前使用JSON來使用Web服務..這裏的JSON:如何從NSDictionary中的HTML字符串顯示UIWebview?

[ 
{ 
id: 4, 
title: "post1", 
permalink: 「http://www.example.com/test-post」, 
content: 「<html> >text here< <iframe src="//player.vimeo.com/video/1010101?title=0&amp;byline=0&amp;portrait=0&amp;color=ff5546" height="638" width="850" allowfullscreen="" frameborder="0"></iframe> <a href="http://example.com/wp-content/uploads/2014/01/testpic.png"><img class="aligncenter size-full wp-image-1180" alt="testpic" src="http://example.co/wp-content/uploads/2014/02/testpic.png" width="850" height="611" /></a>", 
excerpt: " blah blah blah", 
date: "2014-02-24 23:38:19", 
author: "admin", 
categories: 
[ 
"Uncategorized" 
], 
tags: [ ] 
}, 
{ 
id: 1, 
title: "Hello world!", 
permalink: 「http://www.example.com/test-post」, 
content: "Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!", 
excerpt: "Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!", 
date: "2014-02-24 22:46:12", 
author: "admin", 
categories: 
[ 
"Uncategorized" 
], 
tags: [ ] 
} 
] 

,這裏是我的代碼使用方法:

#import "SecondViewController.h" 
#import "AFNetworking.h" 
#import "ThirdViewController.h" 

@interface SecondViewController() 

@end 

@implementation SecondViewController 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.jsonfeed = [[NSArray alloc] init]; 

    [self getJsonFeed]; 


    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

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

-(void)getJsonFeed 
{ 
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
    manager.responseSerializer = [AFJSONResponseSerializer serializer]; 
    [manager GET:@"http://example.com/feed/json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { 

     self.jsonfeed = responseObject; 
     [self.tableView reloadData]; 
     NSLog(@"%@", responseObject); 

    } failure:nil]; 


} 


#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
#warning Potentially incomplete method implementation. 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
#warning Incomplete method implementation. 
    // Return the number of rows in the section. 
    return [self.jsonfeed count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    // Configure the cell... 

    NSDictionary *tempDictionary= [self.jsonfeed objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [tempDictionary objectForKey:@"title"]; 

    return cell; 
} 

#pragma mark - Prepare For Segue 

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
    ThirdViewController *destinationViewController = (ThirdViewController *)segue.destinationViewController; 
    destinationViewController.jsonData = [self.jsonfeed objectAtIndex:indexPath.row]; 

} 

@end 

我想是這樣的:

NSString *myHTML = [NSString stringWithFormat:@"%@", _jsonData]; 
[webView loadHTMLString:myHTML baseURL:nil]; 

但我總是得到這個錯誤:

'[<ThirdViewController 0x8a265a0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key storyboard.' 

我一直被困在一整天試圖找出它..

有什麼建議嗎?

回答

0

首先,我不認爲你的錯誤有事情做與web視圖,如果你看看這裏:

Error: this class is not key value coding-compliant for the key projectToolBar?

這傢伙有同樣的問題,在故事-bord的連接。

第二,我通常用來顯示html上下文的是「RTLabel」類,非常容易使用!它效果很好!

https://github.com/honcheng/RTLabel

所有你需要做的是進口: #進口 「RTLabel.h」

並使用它像這樣:

  finalTakeTrhee = [NSString stringWithFormat:@"<font face='Helvetica-Light' size=15 color='black'>%@</font> ",finalTwo]; 
     } 


    RTLabel *label = [[RTLabel alloc] initWithFrame:CGRectMake(90, 3, 236, 70)]; 
    label.tag = MY_CUSTOM_TAG; 
    label.lineSpacing = -1; 
    label.textAlignment = NSTextAlignmentLeft; 

    [cell.contentView addSubview:label] ; 

    // [cell.contentView addSubview:label]; 
    [label setText:finalTakeTrhee]; 

希望這有助於