0

I am making a simple application using a sqlite database. I am getting table attributes by using this code:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

        [self checkAndCreateDatabase];


        [self readItemsFromDatabaseforTable:@"allcategories"];


        [window addSubview:tabBarController.view];
        [window makeKeyAndVisible];

        return YES;
    }




    -(void) readItemsFromDatabaseforTable:(NSString *)tableName {
        // Setup the database object
        sqlite3 *database;

        // Init the animals Array
        itemsList = [[NSMutableArray alloc] init];

        // Open the database from the users filessytem
        if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
            // Setup the SQL Statement and compile it for faster access
            NSString *sql_str = [NSString stringWithFormat:@"select * from %@", tableName];

            const char *sqlStatement = (char *)[sql_str UTF8String];
            NSLog(@"query %s",sqlStatement);
            //const char *sqlStatement = "select * from allcategories" ;
            sqlite3_stmt *compiledStatement;
            if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
                // Loop through the results and add them to the feeds array
                while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                    // Read the data from the result row
                    NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                    NSInteger aDescription =(compiledStatement, 2);
                    //  NSString *aImageUrl = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];

                    // Create a new animal object with the data from the database

                    Category *item = [[Category alloc] initWithName:aName Quantity:aDescription];

                    // Add the animal object to the animals Array
                    [itemsList addObject:item];

                    [item release];
                }
            }
            // Release the compiled statement from memory
            sqlite3_finalize(compiledStatement);

        }
        sqlite3_close(database);
        NSLog(@"mmm %d",[itemsList count]);

    }

I write this code in the appdelegate.m class.

I retrieve this in myclass.m to display in my table using this code:

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


    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        MyGroceryListAppDelegate *appDelegate = (MyGroceryListAppDelegate *)[[UIApplication sharedApplication] delegate];
        return appDelegate.itemsList.count;
    }


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *CellIdentifier = @"Cell";

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

        // Set up the cell
        MyGroceryListAppDelegate *appDelegate = (MyGroceryListAppDelegate *)[[UIApplication sharedApplication] delegate];

        NSLog(@"ooo %d",[appDelegate.itemsList count]);
        Category *obj = (Category *)[appDelegate.itemsList objectAtIndex:indexPath.row];


        [cell setText:obj.name];
        return cell;
    }

code for my dropdown is

-(void)dropDownCellSelected:(NSInteger)returnIndex{

    [categorySpinner setTitle:[appdelegate.categoriesList objectAtIndex:returnIndex] forState:UIControlStateNormal];
    MyGroceryListAppDelegate *obj = [[MyGroceryListAppDelegate alloc]init];
    [obj checkAndCreateDatabase];
    [obj readItemsFromDatabaseforTable:[appdelegate.categoriesList objectAtIndex:returnIndex]];

    [listOfItems reloadData];
}

It works fine. However, I am passing a tablename which is selected using a dropdown and reloading the tableview, but the data is not reloaded.

I've checked in the console and the array count is changed in appdelegate.m but not changed here:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

I don't understand why it is not changed, I am using the same NSMutable array which is in appdelegate.

(I will be happy to clarify in the comments if anyone has any questions)

4 Answers 4

0

I assume you are calling the readItemsFromDatabaseforTable: method somewhere after the tablename is selected from the dropdown and that code just isn't shown above. After you call this you need to call the following:

[self.tableView reloadData];
[self.tableView flashScrollIndicators];

(The flashScrollIndicators is not necessary, but adds a nice visual clue that the table was reloaded)

As a side note, your itemsList array will leak since you are reallocating it every time. You should allocate this elsewhere and then in your readItemsFromDatabaseforTable: call (instead of the alloc):

[itemsList removeAllObjects];

You are creating a whole new delegate object, which I don't understand, you also seem to be calling reloadData on some other object besides your tableview. You should update your dropDownCellSelected: method like this:

-(void)dropDownCellSelected:(NSInteger)returnIndex{

    [categorySpinner setTitle:[appdelegate.categoriesList objectAtIndex:returnIndex] forState:UIControlStateNormal];
    MyGroceryListAppDelegate *obj = (MyGroceryListAppDelegate *)[[UIApplication sharedApplication] delegate];
    [obj readItemsFromDatabaseforTable:[appdelegate.categoriesList objectAtIndex:returnIndex]];

    [self.tableView reloadData];
}
5
  • ya i forget to add this,i am reload my tableview,but no use.It is not reloaded.Here array is not updated.NSLog(@"ooo %d",[appDelegate.itemsList count]); this value is always value is not changed.For example at launching time 250,after changing table name is not changed but same array at appdelegate count is changed. Mar 17, 2011 at 17:43
  • @MaheshBabu, I don't believe the problem is in the code you have posted, would you edit your question to include the code where you are calling the readItemsFromDatabaseforTable: method? Mar 17, 2011 at 17:48
  • can u please see my updated question.I think it is clear to understand Mar 17, 2011 at 18:04
  • @MaheshBabu I have updated my answer with code I believe should fix your issue. Mar 17, 2011 at 18:15
  • Thank u theChrisKent,i have resolve my problem by placing similar code.As i posted as my answer. Mar 17, 2011 at 18:26
0

I think appDelegate.itemsList.count; should be [appDelegate.itemsList count];

     - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
            MyGroceryListAppDelegate *appDelegate = (MyGroceryListAppDelegate *)[[UIApplication sharedApplication] delegate];
            return [appDelegate.itemsList count];


}
3
  • This code is equivalent, and, as indicated by the OP, is working. Mar 17, 2011 at 17:37
  • @theChrisKent :Can we access the object method like property ? Mar 17, 2011 at 17:43
  • yes, there is an example of this in the LocateMe example from Apple in the very same delegate method. Mar 17, 2011 at 17:46
0

did you reload data of UITableView ? like

[self.tableView reloadData]
1
  • ya,but not reloaded.And array data is not reloaded Mar 17, 2011 at 18:05
0

i resolve my problem as changing the code at -(void)dropDownCellSelected:(NSInteger)returnIndex{ as fallows

 MyGroceryListAppDelegate *appDelegate = (MyGroceryListAppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate checkAndCreateDatabase];
    [appDelegate readItemsFromDatabaseforTable:[appDelegate.categoriesList objectAtIndex:returnIndex]];
    [listOfItems reloadData];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.