Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ghousemd

macrumors newbie
Original poster
Jun 25, 2011
21
0
HI everybody, i am creating a tabbar application with fourh tab as a table view.when i used below code it is not displaying anything and at the same time when i used same code in a separate view based application.it is working correctly. where root view is displaying the table.i don't know what is wrong in this code.plz help me.

Code:
#import <UIKit/UIKit.h>
#import"tabAppDelegate.h"
@class extens;
@interface TableView1 : UIViewController {
    NSMutableArray *aray;NSMutableArray *dag;
    
	NSMutableArray *arrBooks;
    extens *trx;
    UIWindow *window;
    tabAppDelegate *appDelegate;
}
@property(nonatomic,retain)NSMutableArray *aray;
@property(nonatomic,retain)NSMutableArray *dag;
@property(nonatomic,retain)IBOutlet UITableView *myTable;
@property(nonatomic,retain)NSDictionary *book;
@property(nonatomic,retain)extens *trx;
@property(nonatomic,retain) UIWindow *window;
@property(nonatomic,retain)NSMutableDictionary *nameIndexesDictionary,*elementsDictionary ;

@end


#import "TableView1.h"
#import "BookBO.h"
#import "Detail.h"
#import "extens.h"


@implementation TableView1
@synthesize aray,dag,myTable,book,window,nameIndexesDictionary,elementsDictionary ;
- (id)init {
    return [self initWithStyle:UITableViewStyleGrouped];
}


- (id)initWithStyle:(UITableViewStyle)style {
    if (self = [super initWithStyle:style]) {
        self.title = @"Unit Converter";
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (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
{
   
appDelegate = (tabAppDelegate *)[[UIApplication sharedApplication] delegate];
	self.title = @"Coffee List";
    NSLog(@"%@",[appDelegate.coffeeArray count]) ;
    } 
    


- (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 [dag count];
    //return [arrBooks count];
   return [appDelegate.coffeeArray count];
}

- (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];
    }
	
	/*BookBO *ob = (BookBO *)[arrBooks objectAtIndex:indexPath.row];
	cell.textLabel.text = ob.strName;*/
	extens *coffeeObj = [appDelegate.coffeeArray objectAtIndex:indexPath.row];
	
	//Set the coffename.
	cell.text = coffeeObj.name;
    
	//cell.textLabel.text = ob.strCountry;
    
	return cell;
    
}
   
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    
    Detail *detailViewController = [[Detail alloc] initWithNibName:@"Detail" bundle:nil];
    // ...
    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
    
}   // Do any additional setup after loading the view from its nib.


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#import <UIKit/UIKit.h>
@class TableView1;
@interface tabAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
NSMutableArray *coffeeArray;
    TableView1 *tab;
}
/*
 @property (nonatomic, retain) IBOutlet UIWindow *window;
 @property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
 - (void) removeCoffee:(Coffee *)coffeeObj;
 - (void) addCoffee:(Coffee *)coffeeObj;*/
@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@property (nonatomic, retain) IBOutlet TableView1 *tab;
@property (nonatomic, retain) NSMutableArray *coffeeArray;

- (void) copyDatabaseIfNeeded;
- (NSString *) getDBPath;
@end


#import "tabAppDelegate.h"
#import "extens.h"
#import "TableView1.h"
@implementation tabAppDelegate


@synthesize window=_window,tab;

@synthesize tabBarController=_tabBarController;
@synthesize coffeeArray;


- (BOOL)application :  (UIApplication *)application didFinishLaunchingWithOptions:       (NSDictionary *)launchOptions
{
    [self copyDatabaseIfNeeded];
	
	//Initialize the coffee array.
	NSMutableArray *tempArray = [[NSMutableArray alloc] init];
	self.coffeeArray = tempArray;
	[tempArray release];
	
	//Once the db is copied, get the initial data to display on the screen.
	[extens getInitialDataToDisplay:         [self getDBPath]];
	
	// Configure and show the window
	
    // Override point for customization after application launch.
    // Add the tab bar controller's current view as a subview of the window
    self.window.rootViewController = self.tabBarController;
    //[self.window.rootViewController addSubview:nac];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:         (UIApplication *)application
{
    
}

- (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
{
    [self.coffeeArray makeObjectsPerformSelector:@selector(saveAllData)];
	
	[extens finalizeStatements];/*
     Called when the application is about to terminate.
     Save data if appropriate.
     See also applicationDidEnterBackground:.
     */
}
- (void) copyDatabaseIfNeeded {
	
	//Using NSFileManager we can perform many file system operations.
	NSFileManager *fileManager = [NSFileManager defaultManager];
	NSError *error;
	NSString *dbPath = [self getDBPath];
	BOOL success = [fileManager fileExistsAtPath:dbPath]; 
	
	if(!success) {
		
		NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"tutorial.sql"];
		success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
		
		if (!success) 
			NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
	}	
}

- (NSString *) getDBPath {
	
	//Search for standard documents using NSSearchPathForDirectoriesInDomains
	//First Param = Searching the documents directory
	//Second Param = Searching the Users directory and not the System
	//Expand any tildes and identify home directories.
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
	NSString *documentsDir = [paths objectAtIndex:0];
	return [documentsDir stringByAppendingPathComponent:@"tutorial.sql"];
}

/*- (void) removeCoffee:(Coffee *)coffeeObj {
	
	//Delete it from the database.
	[coffeeObj deleteCoffee];
	
	//Remove it from the array.
	[coffeeArray removeObject:coffeeObj];
}

- (void) addCoffee:(Coffee *)coffeeObj {
	
	//Add it to the database.
	[coffeeObj addCoffee];
	
	//Add it to the coffee array.
	[coffeeArray addObject:coffeeObj];
}*/
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    
    //Save all the dirty coffee objects and free memory.
    [self.coffeeArray makeObjectsPerformSelector:@selector(saveAllData)];
}




- (void)dealloc
{
    [_window release];
    [_tabBarController release];
    [coffeeArray release];
    [super dealloc];
}


#import <Foundation/Foundation.h>
#import "BookBO.h"
#import "sqlite3.h"

@interface extens : NSObject {
    NSInteger coffeeID;
	NSString *coffeeName;
	NSDecimalNumber *price;
	UIImage *coffeeImage; 
	
	//Intrnal variables to keep track of the state of the object.
	BOOL isDirty;
	BOOL isDetailViewHydrated;
}

@property (nonatomic, readonly) NSInteger coffeeID,itemid;
@property (nonatomic, copy) NSString *coffeeName,*name,*itemcontent;
@property (nonatomic, copy) NSDecimalNumber *price;
@property (nonatomic, retain) UIImage *coffeeImage;

@property (nonatomic, readwrite) BOOL isDirty;
@property (nonatomic, readwrite) BOOL isDetailViewHydrated;

//Static methods.
+ (void) getInitialDataToDisplay:(NSString *)dbPath;
//Instance methods.
- (id) initWithPrimaryKey:   (NSInteger) pk;

@end


#import "extens.h"
#import "BookBO.h"
#import "tabAppDelegate.h"


static sqlite3 *database = nil;

@implementation extens


@synthesize coffeeID,coffeeName, price, isDirty, isDetailViewHydrated, coffeeImage,itemid,name,itemcontent;

+ (void) getInitialDataToDisplay:(NSString *)dbPath {
	
	tabAppDelegate *appDelegate = (tabAppDelegate *)[[UIApplication sharedApplication] delegate];
	
	if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
		
		//const char *sql = "select coffeeID, coffeeName from bat";
        const char *sql = "select itemid, name from tutorial";
		sqlite3_stmt *selectstmt;
		if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
			
			while(sqlite3_step(selectstmt) == SQLITE_ROW) {
				
				NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
				extens *coffeeObj = [[extens alloc] initWithPrimaryKey:primaryKey];
  // coffeeObj.coffeeName = [NSString stringWithUTF8String:   
(char *)sqlite3_column_text(selectstmt, 1)];
coffeeObj.name = [NSString stringWithUTF8String:     (char*)sqlite3_column_text(selectstmt, 1)];
                NSLog(@"%@",coffeeObj.name);
				coffeeObj.isDirty = NO;
				
				[appDelegate.coffeeArray addObject:coffeeObj];
				[coffeeObj release];
			}
		}
	}
	else
		sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}


- (id) initWithPrimaryKey  :   (NSInteger)  pk {
	
	[super init];
	//coffeeID = pk;
	itemid = pk;

	
   	isDetailViewHydrated = NO;
	
	return self;
}

- (void) dealloc {
	
	
	[name release];
	[super dealloc];
}
@end
 
Last edited by a moderator:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.