- 我在应用程序中有 a1.html、a2.html、b1.html、b2.html 作为本地资源。
- 我无法使用 plist 和 tableview 将这些 html 文件加载到 detailview 的 webview 中
列表
主题.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>HN</key>
<array>
<dict>
<key>Name</key>
<string>Boston</string>
<key>File</key>
<string>a1</string>
</dict>
<dict>
<key>Name</key>
<string>Massachussets</string>
<key>File</key>
<string>a2</string>
</dict>
</array>
<key>LEA</key>
<array>
<dict>
<key>Name</key>
<string>Birmingham</string>
<key>File</key>
<string>b1</string>
</dict>
<dict>
<key>Name</key>
<string>Alabama</string>
<key>File</key>
<string>b2</string>
</dict>
</array>
表视图
TopicsTableViewController.m
#import "TopicsTableViewController.h"
#import "TopicDisplayViewController.h"
#define HNSection 0
#define LEASection 1
#define numberOfSections 2
@interface TopicsTableViewController ()
@property (nonatomic, retain) NSMutableDictionary *topicslist;
@end
@implementation TopicsTableViewController
@synthesize topicslist;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// CUSTOMIZE INITIATION
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"topics" ofType:@"plist"];
TopicsDict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.HNArray = TopicsDict[@"HN"];
self.LEAArray= TopicsDict[@"LEA"];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section {
switch (section){
case HNSection:
return @"NorthEast";
break;
case LEASection:
return @"South";
break;
default:
return 0;
}
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch (section)
{
case HNSection:
return [self.HNArray count];
break;
case LEASection:
return [self.LEAArray count];
break;
default:
return 0;
}
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
switch (indexPath.section)
{
case HNSection:
cell.textLabel.text = self.HNArray[indexPath.row][@"Name"];
break;
case LEASection:
cell.textLabel.text = self.LEAArray[indexPath.row][@"Name"];
break;
default:
return 0;
}
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TopicDisplayViewController*destinationController ;
NSString*file;
switch (indexPath.section)
{
case HNSection:
file = [[NSString alloc] initWithFormat:@"%@", self.HNArray[indexPath.row][@"File"]];
destinationController.htmlfile = self.HNArray[indexPath.row][@"File"];
break;
case LEASection:
file = [[NSString alloc] initWithFormat:@"%@", self.LEAArray[indexPath.row][@"File"]];
destinationController.htmlfile = self.LEAArray[indexPath.row][@"File"];
break;
}
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath: (NSIndexPath *)indexPath {
[self tableView:tableView didSelectRowAtIndexPath:indexPath];
}
@end
详细视图
TopicDisplayViewController.h
#import <UIKit/UIKit.h>
@interface TopicDisplayViewController : UIViewController {
IBOutlet UIWebView*topicview;
NSString*htmlfile;
}
@property (retain) NSString*htmlfile;
@property (nonatomic) UIWebView*topicview;
@end
TopicDisplayViewController.m
#import "TopicDisplayViewController.h"
@interface TopicDisplayViewController ()
@end
@implementation TopicDisplayViewController
@synthesize htmlfile;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
NSString *bundle = [[NSBundle mainBundle] bundlePath];
NSString *webPath = [bundle stringByAppendingPathComponent:htmlfile];
[topicview loadRequest:[NSURLRequest requestWithURL:
[NSURL fileURLWithPath:webPath]]];
}
@end