Adding html files to a table view in Xcode - ios

I am very new to iOS programming. I am currently working on an app using storyboard, and I want to be able to display a series of 7 HTML files in a table, which when clicked on each one will then display an HTML file of formatted text.
I have googled to the ends of the earth and back and cannot find anything that answers my question on how to achieve this.
Basically the app has 4 tabs, the first is text, the second is the one where I want to display html files in a table (called Toolbox), the third is a web browser and the last is text.
I'm pretty sure I have my view controllers where they should be, but loading the html files into the table is not happening. I'm not getting any errors when building, I just get a blank table view on the tab when I run the app in simulator.
I'm not sure what other info I need to provide for anyone to help - below is a list of relevant files, and the code from the relevant h/m files. (I can't post images as yet)
Any help or advice would be greatly appreciated
List of Files:
AppDelegate.h
AppDelegate.m
globals.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 contains the following:
#import < UIKit/UIKit.h>
NSInteger articleIndex;
#interface AppDelegate : UIResponder < UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#end
AppDelegate.m contains the following:
#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 contains the following:
#ifndef TabAT_globals_h
#define TabAT_Viewer_globals_h
NSInteger articleIndex;
#endif
ToolboxViewController.h contains the following:
#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 contains the following:
#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];
*/

Your implementation of ToolboxViewController.m is incomplete. In your code both numberOfSectionsInTableView: and numberOfRowsInSection: are returning 0, which is why nothing is appearing in your table. From what I can tell of your app's description they should return 1 and the length of your articleList array, respectively. Also you will need to set the contents of the table cells in cellForRowAtIndexPath:.
You could do something like the following:
- (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;
}
That should be enough to get you started.

Related

AppDelegate function is not loading Table view controller

I created the simple Master Detail application for login.I deleted the MasterViewController, DetailViewController and Main.storyboard according to this tutorial:
Login app to mysql DB [part 1]
In AppDelegate didFinishLaunchingWithOptions function i made the following change
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]bounds] ];
/*UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
navigationController.topViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem;*/
LoginTableViewController *loginTableViewController=[[LoginTableViewController alloc]initWithNibName:#"LoginTableViewController" bundle:nil ];
self.navigationController =[[UINavigationController alloc]initWithRootViewController:loginTableViewController];
self.window.rootViewController=self.navigationController;
[self.window makeKeyWindow];
return YES;
}
I commented this function from App Delegate function
/*
- (BOOL)splitViewController:(UISplitViewController *)splitViewController collapseSecondaryViewController:(UIViewController *)secondaryViewController ontoPrimaryViewController:(UIViewController *)primaryViewController {
.............................
}*/
I created LoginTableTableViewController with xib of subclass UITableViewController.
I created LoginTableTableViewController.h
#import "LoginTableViewController.h"
#interface LoginTableViewController ()
#end
#implementation LoginTableViewController
#synthesize arraylogin,userNameTextField,passwordTextField;
bool isKeyboardVisible=FALSE;
- (void)viewDidLoad {
[super viewDidLoad];
arraylogin=[[NSArray alloc] initWithObjects:#"user name",#"password",nil];
//set title
self.navigationItem.title=#"Best App";
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardAppeared) name:UIKeyboardDidShowNotification object:nil];
// 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) loginAction
{
if([userNameTextField.text isEqualToString:#""] || [passwordTextField.text isEqualToString:#""])
{
// UIAlertView #alert=[[UIAlertView alloc] intitWithTitle:#"alert" messge:#"Please fill in all //the fields" delegate:self cancel]
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"alert" message:#"Please fill in all the fields" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
//i will use a code to connect to DB turorial
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)keyboardAppeared
{
if(isKeyboardVisible==false)
{
isKeyboardVisible=true;
UIBarButtonItem *btnGo=[[UIBarButtonItem alloc] initWithTitle:#"Go" style:UIBarButtonItemStyleBordered target:self action:#selector(loginAction)];
self.navigationItem.rightBarButtonItem=btnGo;
}
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
return [arraylogin count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifer=#"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer];
if (cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifer];
}
//cell are not selectable
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
CGRect frame;
frame.origin.x=10;
frame.origin.y=10;
frame.size.height=30 ;
frame.size.width= 200;
UILabel *label=[[UILabel alloc]initWithFrame:frame];
label.font=[UIFont boldSystemFontOfSize:16.0];
label.text=[arraylogin objectAtIndex:indexPath.row];
[cell.contentView addSubview:label];
frame.origin.x=110;
frame.size.height=90 ;
frame.size.width= 180;
// Configure the cell
if(indexPath.row==0)
{//username part
userNameTextField=[[UITextField alloc] initWithFrame:frame];
userNameTextField.returnKeyType=UIReturnKeyDefault;
[cell.contentView addSubview:userNameTextField];
}
else{//password part
passwordTextField=[[UITextField alloc] initWithFrame:frame];
passwordTextField.returnKeyType=UIReturnKeyDefault;
passwordTextField.secureTextEntry=YES;
[cell.contentView addSubview:passwordTextField];
}
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:#[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
// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here, for example:
// Create the next view controller.
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:<##"Nib name"#> bundle:nil];
// Pass the selected object to the new view controller.
// Push the view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
}
*/
-(void) viewDidUnload
{
[super viewDidUnload];
self.arraylogin=nil;
self.userNameTextField=nil;
self.passwordTextField=nil;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#end
This program should be executing and show out like in this image
.
Execution stops program on some thread of main class function how i can remove this error and get the required output?You can download sample code from this link for correction.https://drive.google.com/file/d/0B5pNDpbvZ8SnV3Zab3VPM1B0a0k/view?usp=sharing
Your project is still specifying that a "Main" storyboard should be loaded. Edit your Info.plist file to remove the storyboard entry and it will get you past the current error.
(If you include error messages from your debug console when asking a question, it makes it easier on anyone trying to help. For example: "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Could not find a storyboard named 'Main' in bundle".)

iOS official to-do tutorial: List View Controller does not display correctly

I've been trying to learn iOS app development and i started from the official tutorial of Apple from this link: https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/ThirdTutorial.html
It's a 3 step tutorial. I followed each one's instructions as they are but at the end of 3rd tutorial, i cannot get correct output from my app. The tutorial says it should look like this when it's over:
.
But my app looks like this:
.
I cannot add new item. Below you can see my storyboard and all my code. But you can also download whole project from here: Link
If it helps, i'm working on OS X 10.9.2 with Xcode 5.0.2
My storyboard:
XYZAddToDoItemViewController.m file's code:
#import "XYZAddToDoItemViewController.h"
#interface XYZAddToDoItemViewController ()
#property (weak, nonatomic) IBOutlet UITextField *textField;
#property (weak, nonatomic) IBOutlet UIBarButtonItem *doneButton;
#end
#implementation XYZAddToDoItemViewController
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if (sender != self.doneButton) return;
if (self.textField.text.length > 0) {
self.toDoItem = [[XYZToDoItem alloc] init];
self.toDoItem.itemName = self.textField.text;
self.toDoItem.completed = NO;
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
XYZAddToDoItemViewController.h file's code:
#import <UIKit/UIKit.h>
#import "XYZToDoItem.h"
#interface XYZAddToDoItemViewController : UIViewController
#property XYZToDoItem *toDoItem;
#end
XYZToDoItem.m file's code:
#import "XYZToDoItem.h"
#implementation XYZToDoItem
#end
XYZToDoItem.h file's code:
// This class creates and stores the data for individual list items.
#import <Foundation/Foundation.h>
#interface XYZToDoItem : NSObject
#property NSString *itemName;
#property BOOL completed;
#property (readonly) NSDate *creationDate;
#end
XYZToDoListViewController.m file's code:
#import "XYZToDoListViewController.h"
#import "XYZAddToDoItemViewController.h"
#interface XYZToDoListViewController ()
#property NSMutableArray *toDoItems;
#end
#implementation XYZToDoListViewController
- (void)loadInitialData {
XYZToDoItem *item1 = [[XYZToDoItem alloc] init];
item1.itemName = #"Buy milk";
[self.toDoItems addObject:item1];
XYZToDoItem *item2 = [[XYZToDoItem alloc] init];
item2.itemName = #"Buy eggs";
[self.toDoItems addObject:item2];
XYZToDoItem *item3 = [[XYZToDoItem alloc] init];
item3.itemName = #"Read a book";
[self.toDoItems addObject:item3];
}
- (IBAction)unwindToList:(UIStoryboardSegue *)segue
{
XYZAddToDoItemViewController *source = [segue sourceViewController];
XYZToDoItem *item = source.toDoItem;
if (item != nil) {
[self.toDoItems addObject:item];
[self.tableView reloadData];
}
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.toDoItems = [[NSMutableArray alloc] init];
[self loadInitialData];
// 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.
// it changes when i comment following line!!
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#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 [self.toDoItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ListPrototypeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
cell.textLabel.text = toDoItem.itemName;
if (toDoItem.completed) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
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:#[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 - Navigation
// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
//
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
XYZToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row];
tappedItem.completed = !tappedItem.completed;
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
#end
XYZToDoListViewController.h file's code:
#import <UIKit/UIKit.h>
#import "XYZToDoItem.h"
#interface XYZToDoListViewController : UITableViewController
- (IBAction)unwindToList:(UIStoryboardSegue *)segue;
#property XYZToDoItem *toDoItem;
#end
XYZAppDelegate.m file's code:
#import "XYZAppDelegate.h"
#implementation XYZAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
//self.window.backgroundColor = [UIColor whiteColor];
//[self.window makeKeyAndVisible];
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
XYZAppDelegate.h file's code:
#import <UIKit/UIKit.h>
#interface XYZAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#end
I think Apple forgot one step in the tutorial. In order to add items, you must link your done button to the method unwindToList.
All you do is go to your storyboard and Ctrl-click Done and drag it to the Exit icon on your XYZAddToDoItemViewController. A menu "ActionSegue" should appear with your unwindToDoList method.
Just drag the line over that method, then release, and you're done!
XYZToDoListViewController.m file's code: seems to be missing an import for XYZToDoItem.h - It looks like it was added to XYZToDoListViewController.h instead.
You have not followed the tutorial properly. You had make a mistake which I have had done too.
Mistake:
You have used the + symbol in Bar Item's "Title".
Solution:
Select Add From Identifier's drop down list , and re-follow the tutorial again.
It should work!

iOS Novice - viewDidLoad

I'm going through the Apple Start Developing Guide and I have encountered an error in the demo app.
The code is as follows:
#import "XYZToDoListViewController.h"
#import "XYZToDoItem.h"
#interface XYZToDoListViewController ()
#property NSMutableArray *toDoItems;
-(void)viewDidLoad{
[super viewDidLoad];
self.toDoItems = [[NSMutableArray alloc]init];
[self loadInitialData];
}
#end
#implementation XYZToDoListViewController
-(void)loadInitialData{
XYZToDoItem *item1 = [[XYZToDoItem alloc]init];
item1.itemName = #"Buy Milk";
[self.toDoItems addObject:item1];
XYZToDoItem *item2 = [[XYZToDoItem alloc]init];
item2.itemName = #"Go Shopping";
[self.toDoItems addObject:item2];
XYZToDoItem *item3 = [[XYZToDoItem alloc]init];
item3.itemName = #"Wake Up";
[self.toDoItems addObject:item3];
}
- (IBAction)unwindToList:(UIStoryboardSegue *)segue
{
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 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.
}
#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 [self.toDoItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ListPrototypeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
cell.textLabel.text = toDoItem.itemName;
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:#[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 - Navigation
// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
#end
On building it comes up with an error saying it expected ; after viewDidLoad.
I have googled this to see if I can figure out why but can't seem to.
put this in your .h file
#interface className : Class that you inherit
#property NSMutableArray *toDoItems;
#end
of course, you'd need to replace className with your name for the class, and the 'Class that you inherit' with the super classes such as NSObject, UIViewController, UIView, or any other class.
and put this in your .m file
#implementation className
-(void)viewDidLoad{
[super viewDidLoad];
self.toDoItems = [[NSMutableArray alloc]init];
[self loadInitialData];
}
#end
that should help.
Hope it helps :)
It looks like you might either have the property:
#property NSMutableArray *toDoItems;
In the implementation file (.m) or the method:
-(void)viewDidLoad{
...
}
In the header file (.h).
In addition you'll need an #implementation or #interface. Maybe you should update your question with the full code from the file.
The below property should be in .h file. Because in .h file we need to declare the method so property will declare the method which should be in .h file
#property NSMutableArray *toDoItems;
And below will be in .m file because in .m file we need to define the method.
- (void)viewDidLoad
{
[super viewDidLoad];
self.toDoItems = [[NSMutableArray alloc] init];
[self loadInitialData];
}

UITableView with UIWebVIew Not Loading UIWebView. Does It Require an IBAction?

This is the code I've got so far, basically what's supposed to happen is that once you make a selection in the UITableView, you should then get a transition that will take you onto the UIWebView although all that's happening is once you make a selection it just highlights blue.
First view controller .h
#import <UIKit/UIKit.h>
#interface YoMaFifthViewController : UITableViewController
{
NSArray *data, *sites;
}
#end
First view controller .m
#import "YoMaFifthViewController.h"
#import "YoMaWebsiteViewController.h"
#interface YoMaFifthViewController ()
#end
#implementation YoMaFifthViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - view lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
data = [NSArray arrayWithObjects:#"Website", #"Developer", nil];
sites = [NSArray arrayWithObjects:
#"https://www.google.co.uk/",
#"https://www.google.co.uk/",
nil];
// 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;
}
#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 [data count];
}
- (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];
}
// Configure the cell...
cell.textLabel.text = [data objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [sites objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[data objectAtIndex:indexPath.row]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
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:#[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
{
YoMaWebsiteViewController *wvc = [[YoMaWebsiteViewController alloc] initWithNibName:#"YoMaWebsiteViewController" bundle:nil];
wvc.site = [sites objectAtIndex:indexPath.row];
[self.navigationController pushViewController:wvc animated:YES];
}
#end
Website view controller .h
#import <UIKit/UIKit.h>
#interface YoMaWebsiteViewController : UIViewController
{
IBOutlet UIWebView *webview;
}
#property (retain) NSString *site;
#end
Website view controller .m
#import "YoMaWebsiteViewController.h"
#interface YoMaWebsiteViewController ()
#end
#implementation YoMaWebsiteViewController
#synthesize site;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:self.site];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You would need an IB Action or a segue to the website view controller (for static cells). When it comes to dynamic prototypes, a segues would work. You can do the segues with a little bit of code. First, go to the storyboard and Control-drag from the First View Controller to the Website View Controller (use the icons on the bottom or use the document outline). Then, after giving the segue an identifier, use the following code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"yourSegueIdentifier" sender:[self.tableView cellForRowAtIndexPath:indexPath]];
}
Edit 2: If you're using a xib do this (no control drag):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.navigationController pushViewController:viewControllerNameHere animated:YES sender:[self.tableView cellForRowAtIndexPath:indexPath]];
}

How to display custom cells that have been defined in interface builder?

I am trying to embed custom designed TableView Cells (in interface builder). For starters I am trying to add a few text fields in its own section. I am able to show the custom designed tableview however I am doing something incorrect as it looks all messed up. Also not super clear on how to add additional sections. Can someone guide me to right direction? I tried following: http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW20
But got a little lost.
Here is what it looks like:
This is my setup in interface builder:
This is my code:
header file:
#import <UIKit/UIKit.h>
#interface SearchTableViewController : UITableViewController
{
}
#property (nonatomic, retain) IBOutlet UITableViewCell *priceRanceCell;
#end
implementation:
#import "SearchTableViewController.h"
#interface SearchTableViewController ()
#end
#implementation SearchTableViewController
#synthesize priceRanceCell=_priceRanceCell;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Some Search", #"Some Search");
self.tabBarItem.image = [UIImage imageNamed:#"search"];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 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;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#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 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"SearchViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
NSInteger section = [indexPath section];
switch (section) {
case 0: // First cell in section 1
return self.priceRanceCell;
break;
default:
// Do something else here if a cell other than 1,2,3 or 4 is requested
return cell;
break;
}
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:#[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];
*/
}
#end
You need to implement heightForRowAtIndexPath:. Also, note that when you are dequeueReusableCellWithIdentifier:, the result maybe be nil, in which case you need to create a cell. You should modify your logic so you don't even try if its section 0.
In interface builder, click the custom cell you made, memorise the height; then click on the table view you are going to use, enter the row height from your memory.

Resources