开发者

tableview values is not reloaded

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:aDe开发者_如何学Pythonscription];

                    // 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)


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];
}


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];


}


did you reload data of UITableView ? like

[self.tableView reloadData]


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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜