Hi guys,
I've kinda finished my application for a school project but have run into a major "bug".
It's a account management application. I'm unable to insert a picture here so here's a link:
http://i232.photobucket.com/albums/ee112/seelani/Screenshot2010-12-22atPM075512.png
Here's the problem when i click on the plus sign, i push a nav controller to load another view to handle the adding and deleting of categories. When i add and return back to the view above, it doesn't update. It only updates after i hit the button on the right which is another view used to change some settings, and return back to the page. I did some research on viewWillAppear and such but I'm still confused to why it doesn't work properly.
This problem is also affecting my program when i delete a category, and return back to this view it crashes cos the view has not reloaded successfully. I will get this error when deleting and returning to the view. "* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSMutableArray objectAtIndex:]: index 4 beyond bounds [0 .. 3]'".
[EDIT]
Table View Code:
@class LoginViewController;
@implementation CategoryTableViewController
@synthesize categoryTableViewController;
@synthesize categoryArray;
@synthesize accountsTableViewController;
@synthesize editAccountTable;
@synthesize window;
CategoryMgmtTableController *categoryMgmtTableController;
ChangePasswordView *changePasswordView;
- (void) save_Clicked:(id)sender {
/*
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Category Management"
message:@"Load category management table view"
delegate:self
cancelButtonTitle: @"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
*/
KeyCryptAppAppDelegate *appDelegate = (KeyCryptAppAppDelegate *)[[UIApplication sharedApplication] delegate];
categoryMgmtTableController = [[CategoryMgmtTableController alloc]initWithNibName:@"CategoryMgmtTable" bundle:nil];
[appDelegate.categoryNavController pushViewController:categoryMgmtTableController animated:YES];
}
- (void) change_Clicked:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Change Password"
message:@"Change password View"
delegate:self
cancelButtonTitle: @"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
KeyCryptAppAppDelegate *appDelegate = (KeyCryptAppAppDelegate *)[[UIApplication sharedApplication] delegate];
changePasswordView = [[ChangePasswordView alloc]initWithNibName:@"ChangePasswordView" bundle:nil];
[appDelegate.categoryNavController pushViewController:changePasswordView animated:YES];
/*
KeyCryptAppAppDelegate *appDelegate = (KeyCryptAppAppDelegate *)[[UIApplication sharedApplication] delegate];
categoryMgmtTableController = [[CategoryMgmtTableController alloc]initWithNibName:@"CategoryMgmtTable" bundle:nil];
[appDelegate.categoryNavController pushViewController:categoryMgmtTableController animated:YES];
*/
}
#pragma mark -
#pragma mark Initialization
/*
- (id)initWithStyle:(UITableViewStyle)style {
// Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
if ((self = [super initWithStyle:style])) {
}
return self;
}
*/
-(void) initializeCategoryArray {
sqlite3 *db= [KeyCryptAppAppDelegate getNewDBConnection];
KeyCryptAppAppDelegate *appDelegate = (KeyCryptAppAppDelegate *)[[UIApplication sharedApplication] delegate];
const char *sql = [[NSString stringWithFormat:(@"Select Category from Categories;")]cString];
const char *cmd = [[NSString stringWithFormat:@"pragma key = '%@' ", appDelegate.pragmaKey]cString];
sqlite3_stmt *compiledStatement;
sqlite3_exec(db, cmd, NULL, NULL, NULL);
if (sqlite3_prepare_v2(db, sql, -1, &compiledStatement, NULL)==SQLITE_OK)
{
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
[categoryArray addObject:[NSString stringWithUTF8String:(char*) sqlite3_column_text(compiledStatement, 0)]];
}
else {
NSAssert1(0,@"Error preparing statement", sqlite3_errmsg(db));
}
sqlite3_finalize(compiledStatement);
}
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated {
self.title = NSLocalizedString(@"Categories",@"Types of Categories");
categoryArray = [[NSMutableArray alloc]init];
[self initializeCategoryArray];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:@selector(save_Clicked:)] autorelease];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self action:@selector(change_Clicked:)] autorelease];
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
NSLog (@"view did appear");
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
NSLog (@"view will disappear");
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[categoryTableView reloadData];
NSLog (@"view did disappear");
[super viewDidDisappear:animated];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
#pragma mark -
#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.categoryArray count];
}
// Customize the appearance of table view cells.
- (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];
}
// Configure the cell...
NSUInteger row = [indexPath row];
cell.text = [categoryArray objectAtIndex: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:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
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 -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedCategory = [categoryArray objectAtIndex:[indexPath row]];
NSLog (@"AccountsTableView.xib is called.");
if ([categoryArray containsObject: selectedCategory])
{
if (self.accountsTableViewController == nil)
{
AccountsTableViewController *aAccountsView = [[AccountsTableViewController alloc]initWithNibName:@"AccountsTableView"bundle:nil];
self.accountsTableViewController =aAccountsView;
[aAccountsView release];
}
NSInteger row =[indexPath row];
accountsTableViewController.title = [NSString stringWithFormat:@"%@", [categoryArray objectAtIndex:row]];
// This portion pushes the categoryNavController.
KeyCryptAppAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[self.accountsTableViewController initWithTextSelected:selectedCategory];
KeyCryptAppAppDelegate *appDelegate = (KeyCryptAppAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.pickedCategory = selectedCategory;
[delegate.categoryNavController pushViewController:accountsTableViewController animated:YES];
}
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc {
[accountsTableViewController release];
[super dealloc];
}
@end
And the code that i used to delete rows(this is in a totally different tableview):
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
NSString *selectedCategory = [categoryArray objectAtIndex:indexPath.row];
[categoryArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
[deleteCategoryTable reloadData];
//NSString *selectedCategory = [categoryArray objectAtIndex:indexPath.row];
sqlite3 *db= [KeyCryptAppAppDelegate getNewDBConnection];
KeyCryptAppAppDelegate *appDelegate = (KeyCryptAppAppDelegate *)[[UIApplication sharedApplication] delegate];
const char *sql = [[NSString stringWithFormat:@"Delete from Categories where Category = '%@';", selectedCategory]cString];
const char *cmd = [[NSString stringWithFormat:@"pragma key = '%@' ", appDelegate.pragmaKey]cString];
sqlite3_stmt *compiledStatement;
sqlite3_exec(db, cmd, NULL, NULL, NULL);
if (sqlite3_prepare_v2(db, sql, -1, &compiledStatement, NULL)==SQLITE_OK)
{
sqlite3_exec(db,sql,NULL,NULL,NULL);
}
else {
NSAssert1(0,@"Error preparing statement", sqlite3_errmsg(db));
}
sqlite3_finalize(compiledStatement);
}
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
}
}