[英]Can't get data on detailviewcontroller with Hpple parsing
I am a beginner in Obj-C and I try to make an app with this raywenderlich tutorial . 我是Obj-C的初学者,我尝试使用raywenderlich教程制作应用程序。
I parse html page with Title of articles (mininglife.ru) , also I getting an urls of each articles and keep it in an NSMutableArray
for using it in my DetailviewController
. 我用文章标题(mininglife.ru)解析html页面,同时获取每个文章的网址,并将其保存在
NSMutableArray
以便在DetailviewController
使用它。
But when I try to pull urls off and put it in NSUrl
nothing happens. 但是,当我尝试提取网址并将其放入
NSUrl
什么也没发生。 I've tried a lot of ways, Can you help me with it and try to explain if I do something wrong. 我已经尝试了很多方法,您能帮我一下吗,并尝试解释我做错了什么。 Thank you.
谢谢。
-(void)loadArticles{
Articlelist *urlOfArticle = [_objects objectAtIndex:self]; // from previous parsing
NSURL *articleUrl = [NSURL URLWithString:urlOfArticle.url];// I think this string is wrong, or?
NSData *htmlUrlsData = [NSData dataWithContentsOfURL:articleUrl];
TFHpple *articleParser = [TFHpple hppleWithHTMLData:htmlUrlsData];
NSString *articleNameXpath = @"/html/body/div[1]/div/div[1]/main/article/h1";
// NSString *articleContentXpath = @"//div[@class'entry-content']//p";
NSArray *articleNameNodes = [articleParser searchWithXPathQuery:articleNameXpath];
// NSArray *articleContentNodes = [articleParser searchWithXPathQuery:articleContentXpath];
NSMutableArray *newArticleArray = [[NSMutableArray alloc] initWithCapacity:0];
for (TFHppleElement *element in articleNameNodes) {
Article *newArticleName = [[Article alloc] init];
[newArticleArray addObject:newArticleName];
newArticleName.name = [[element firstChild] content];
}
_articleName = newArticleArray;
[self.tableView reloadData];
}
Change interface to this 将界面更改为此
@interface MasterViewController () {
NSMutableArray *_articles;
}
@end
Add this to MasterViewController 将此添加到MasterViewController
- (void)loadArticles
{
NSURL *articlesUrl = [NSURL URLWithString:@"http://mininglife.ru"];
NSData *articlesHtmlData = [NSData dataWithContentsOfURL:articlesUrl];
TFHpple *articlesParser = [TFHpple hppleWithHTMLData:articlesHtmlData];
NSString *articlesXpathQueryString = @"/html/body/div[1]/div/div[1]/main/article/h1";
NSArray *articlesNodes = [articlesParser searchWithXPathQuery:articlesXpathQueryString];
NSMutableArray *newArticles = [[NSMutableArray alloc] initWithCapacity:0];
for (TFHppleElement *element in articlesNodes) {
Article *article = [[Article alloc] init];
[newArticles addObject:article];
article.title = [[element firstChild] content];
article.url = [element objectForKey:@"href"];
}
_articles = newArticles;
[self.tableView reloadData];
}
Change DetailviewController.h's interface to 将DetailviewController.h的界面更改为
@class Article;
@interface DetailViewController : UIViewController
@property (strong, nonatomic) Article *article;
@property (strong, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
@end
Change DetailViewController.m to 将DetailViewController.m更改为
#import "DetailViewController.h"
#import "Article.h"
#import "TFHpple.h"
@implementation DetailViewController
@synthesize detailDescriptionLabel = _detailDescriptionLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadArticle];
}
- (void)loadArticle
{
NSURL *articleUrl = [NSURL URLWithString:self.article.url];
NSData *articleHtmlData = [NSData dataWithContentsOfURL:articleUrl];
TFHpple *articleParser = [TFHpple hppleWithHTMLData:articleHtmlData];
NSString *articleXpathQueryString = @"//div[@class'entry-content']//p";
NSArray *articleNodes = [articleParser searchWithXPathQuery:articleXpathQueryString];
NSMutableArray *newArticleContents = [[NSMutableArray alloc] initWithCapacity:0];
for (TFHppleElement *element in articleNodes) {
[newArticleContents addObject:[[element firstChild] content]];
}
NSLog(@"%@", newArticleContents);
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"Detail", @"Detail");
}
return self;
}
@end
Don't forget to change all call to loadTutorials to loadArticles in MasterViewController. 不要忘记将对loadTutorials的所有调用更改为MasterViewController中的loadArticles。 PS: This example only prints the articles content to the console
PS:此示例仅将文章内容打印到控制台
Your xPath might be wrong as well 您的xPath也可能是错误的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.