2012-01-27 55 views
0

我对iOS编程非常新颖。我目前正在使用故事板开发一个应用程序,并且我希望能够在表格中显示一系列7个HTML文件,点击每个文件后将显示格式化文本的HTML文件。将HTML文件添加到Xcode中的表格视图

我已经搜索到了地球的尽头,并且找不到任何回答我如何实现这个问题的问题。

基本上应用程序有4个选项卡,第一个是文本,第二个是我想在表格中显示html文件(称为工具箱)的地方,第三个是Web浏览器,最后是文本。

我很确定我有我的视图控制器他们应该在哪里,但加载html文件到表中没有发生。我在构建时没有收到任何错误,当我在模拟器中运行应用程序时,我只是在选项卡上获得空白表视图。

我不确定我需要提供哪些其他信息以帮助任何人 - 下面是相关文件的列表以及相关h/m文件中的代码。 (我不能发表图片尚未)

任何帮助或建议将不胜感激

文件列表中:

AppDelegate.h

AppDelegate.m

全局。 h

MainStoryboard_iPhone.storyboard

MainStoryboard_iPad.storyboard

ToolboxViewController.h

ToolboxViewController.m

chapter1.html

chapter2.html

chapter3.html

chapter4.html

chapter5.html

chapter6.html

chapter7.html

AppDelegate.h包含以下内容:

#import < UIKit/UIKit.h> 
NSInteger articleIndex; 
@interface AppDelegate : UIResponder < UIApplicationDelegate> 
@property (strong, nonatomic) UIWindow *window; 
@end 

AppDelegate.m包含以下内容:

#import "AppDelegate.h" 
#import "ToolboxViewController.h" 
@implementation AppDelegate { 
NSArray *articleList; 
} 
@synthesize window = _window; 
- (void)dealloc 
{ 
[_window release]; 
[super dealloc]; 
} 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:   (NSDictionary *)launchOptions 
{ 
articleList = [[NSArray arrayWithObjects: 
       @"chapter1", 
       @"chapter2", 
       @"chapter3", 
       @"chapter4", 
       @"chapter5", 
       @"chapter6", 
       @"chapter7", 
       nil] retain]; 

return YES; 
} 

- (void)applicationWillResignActive:(UIApplication *)application 
{ 
/* 
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 
*/ 
} 

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
/* 
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
*/ 
} 

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
/* 
Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 
*/ 
} 

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
/* 
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
*/ 
} 

- (void)applicationWillTerminate:(UIApplication *)application 
{ 
/* 
Called when the application is about to terminate. 
Save data if appropriate. 
See also applicationDidEnterBackground:. 
*/ 
} 

@end 

globals.h包含以下内容:

#ifndef TabAT_globals_h 
#define TabAT_Viewer_globals_h 
NSInteger articleIndex; 
#endif 

ToolboxViewController。h包含以下内容:

#import <UIKit/UIKit.h> 
#import "globals.h" 
@interface ToolboxViewController : UITableViewController 
{ 
NSArray *articleList; 
NSArray *articleNames; 
} 
@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController; 
@end 

ToolboxViewController.m包含以下内容:

#import "ToolboxViewController.h" 
#import "globals.h" 
@implementation ToolboxViewController 
@synthesize window = _window; 
@synthesize navigationController = _navigationController; 
- (id)initWithStyle:(UITableViewStyle)style 
{ 
self = [super initWithStyle:style]; 
if (self) { 
    // Custom initialization 
} 
return self; 
} 
- (void)didReceiveMemoryWarning 
{ 
// Releases the view if it doesn't have a superview. 
[super didReceiveMemoryWarning]; 

// Release any cached data, images, etc that aren't in use. 
} 
#pragma mark - View lifecycle 
- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

articleNames = [[NSArray arrayWithObjects: 
       @"chapter1", 
       @"chapter2", 
       @"chapter3", 
       @"chapter4", 
       @"chapter5", 
       @"chapter6", 
       @"chapter7", 
       nil] retain]; 
articleList = [[NSArray arrayWithObjects: 
       @"What is Anxiety", 
       @"Anxiety Symptoms", 
       @"Coping Skills", 
       @"Visualisation", 
       @"Treatment", 
       @"Places to go for help", 
       @"About", 
       nil] retain]; 


// 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)viewDidUnload 
{ 
[super viewDidUnload]; 
// Release any retained subviews of the main view. 
// e.g. self.myOutlet = nil; 
} 
- (void)viewWillAppear:(BOOL)animated 
{ 
[super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
[super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
[super viewWillDisappear:animated]; 
} 
- (void)viewDidDisappear:(BOOL)animated 
{ 
[super viewDidDisappear:animated]; 
} 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
// Return YES for supported orientations 
return YES; 
} 

#pragma mark - Table view data source 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
// Return the number of sections. 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
// Return the number of rows in the section. 
return [articleList count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"ToolboxCell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 

// Configure the cell... 

cell.textLabel.text = [articleList objectAtIndex:indexPath.row]; 

return cell; 
} 

/* 
// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
// Return NO if you do not want the specified item to be editable. 
return YES; 
} 
*/ 

/* 
// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if (editingStyle == UITableViewCellEditingStyleDelete) { 
    // Delete the row from the data source 
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
} 
else if (editingStyle == UITableViewCellEditingStyleInsert) { 
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
} 
} 
*/ 
/* 
// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
} 
*/ 
/* 
// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
// Return NO if you do not want the item to be re-orderable. 

return YES; 
} 
*/ 

#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
// Navigation logic may go here. Create and push another view controller. 
/* 
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 
// ... 
// Pass the selected object to the new view controller. 
[self.navigationController pushViewController:detailViewController animated:YES]; 
[detailViewController release]; 
*/ 

回答

0

你实现ToolboxViewController.m是不完整的。在你的代码都numberOfSectionsInTableView:numberOfRowsInSection:正在返回0,这就是为什么没有什么是你的表出现。从我可以告诉你应用程序的描述中,他们应该分别返回1和你的articleList数组的长度。您还需要设置cellForRowAtIndexPath:中表格单元格的内容。

你可以做类似如下:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [articleList length]; 
} 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // here's where you set the content inside your table cell 
    cell.textLabel.text = [articleList objectAtIndex:[indexPath row]]; 

    return cell; 
} 

这应该足以让你开始。

+0

感谢jonkroll,这无疑帮助了很多,我现在已经设法显示人名章的表,我不再看到任何警告,所以非常感谢你为。出于某种原因,尽管当我点击模拟器中的任何章节时,没有任何动作,这是否是由于出口不足或错误造成的? (PS已经更新上面的代码) – Kiwiboy 2012-01-27 05:40:55

+0

当你点击一个表格单元格的消息'didSelectRowAtIndexPath方法:'发送到您的视图控制器。您需要提供该方法中的代码才能让您的应用执行相应的操作。通常这个动作是推动导航堆栈上的另一个视图控制器。在上面的代码中,您可以看到XCode为该方法提供了一个有用的但已注释掉的存根。你应该看看表视图编程指南适用于iOS(http://developer.apple.com/library/IOs/#documentation/UserExperience/Conceptual/TableView_iPhone/AboutTableViewsiPhone/AboutTableViewsiPhone.html) – jonkroll 2012-01-27 05:58:13