项目类型:masterview应用程序
组件第一个视图:Tableview
第二个视图:webview我想要的是在这样一种技术中导航:当用户点击一个单元格时,它应该得到href
属性并在下一个webview中打开,它链接到的页面...而不仅仅是href
属性
建议
谢谢
#import "MasterViewController.h"
#import "TFHpple.h"
#import "TFHppleElement.h"
#import "Preface.h"
#import "Chapters.h"
#import "Index.h"
#import "DetailViewController.h"
@interface MasterViewController ()
{
NSMutableArray * preface;
NSMutableArray * chapters;
NSMutableArray * index;
}
@end
@implementation MasterViewController
- (void) loadChapters
{
///// capturing the link /////
/*
NSURL pointer to store the file url
*/
NSURL * chapter_url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test/mainpage" ofType:@"html"] isDirectory:NO];
NSData * chapter_data = [NSData dataWithContentsOfURL:chapter_url];
///// passing to TFHpple /////
TFHpple * chapter_parser = [TFHpple hppleWithHTMLData:chapter_data];
///// String for XPath Query /////
NSString * chapter_query_string = @"//div[@class='cD']/ul[2]/li/a";
NSArray * chapter_nodes = [chapter_parser searchWithXPathQuery:chapter_query_string];
///// Array for initializing elements of preface/////
NSMutableArray * chapter_contents = [[NSMutableArray alloc]initWithCapacity:0];
///// Now looping to fetch contents /////
for (TFHppleElement * element in chapter_nodes) {
Chapters * chapter_ptr =[[Chapters alloc]init];
[chapter_contents addObject:chapter_ptr];
chapter_ptr.chapter_title = [[element firstChild]content];
chapter_ptr.chapter_url = [element objectForKey:@"href"];
}
chapters= chapter_contents;
[self.tableView reloadData];
}
viewdidload
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadPreface];
[self loadChapters];
[self loadIndex];
// Do any additional setup after loading the view, typically from a nib.
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
switch (section) {
case 0:
return @"Table of Contents";
break;
case 1:
return @"Chapters";
break;
case 3:
return @"Index";
break;
}
return nil;
}
方法在部分中返回no行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case 0:
return preface.count;
break;
case 1:
return chapters.count;
break;
case 2:
return index.count;
break;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
switch (indexPath.section) {
case 0:
{
Preface *preface_access = [preface objectAtIndex:indexPath.row];
cell.textLabel.text = preface_access.preface_title;
return cell;
}
case 1:
{
Chapters * chapter_access = [chapters objectAtIndex:indexPath.row];
cell.textLabel.text = chapter_access.chapter_title;
return cell;
}
case 2:
{
Index * index_access = [index objectAtIndex:indexPath.row];
cell.textLabel.text = index_access.index_title;
return cell;
}
default:
break;
}
return cell;
我想做这样的事情
在 detailviewcontroller.m 中
它是第二个视图控制器
这是伪代码我在OOP概念中很弱,所以appologies
- (void)viewDidLoad
{
NSMutableArray * contentsfromprevious = [[NSMutableArray alloc]initWithCapacity:0];
Preface * data = [[Preface alloc]init];
data.preface_url = contentsfromprevious;
data = preface_contents;
[self.loadwebview.request];
}
答案 0 :(得分:2)
首先,你的问题仍然不够明确。我已经明白了,你想要这样的东西:
假设这样,你应该在MasterViewController上实现这个UITableViewDelegate方法:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailVC = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
detailVC.htmlFileName = [htmlFiles objectAtIndex:indexPath.row]; // you might need to change this according to your need, as I said your requirement is unclear
[self.navigationController pushViewController: detailVC animated:YES];
}
此外,您还需要将其添加到DetailViewController.h
文件
@property (nonatomic, strong) NSString *htmlFileName;
在您完成此操作后,您应该将DetailViewControllers viewDidLoad方法更改为以下内容:
- (void)viewDidLoad
{
// your code
// ....
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: htmlFileName ofType:@"html"] isDirectory:NO]]];
webView.scalesPageToFit = YES;
// your code
// ...
}
我不确定这是否是你真正想要的。另一方面,您不必在break
个语句之后放置return
语句。它永远不会被执行。