tableview值未重新加载

时间:2011-03-17 17:24:08

标签: iphone objective-c ios uitableview

我正在使用sqlite数据库创建一个简单的应用程序。我使用此代码获取表属性:

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

我的下拉列表的代码是

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

工作正常。但是,我传递的是一个使用下拉列表并重新加载tableview的tablename,但是没有重新加载数据。

我已经在控制台中检查了数组计数在appdelegate.m中已更改但未在此处更改:

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

我不明白为什么它没有改变,我使用的是appdelegate中相同的NSMutable数组。

(如果有人有任何问题,我很乐意在评论中澄清)

4 个答案:

答案 0 :(得分:0)

我认为appDelegate.itemsList.count;应为[appDelegate.itemsList count];

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


}

答案 1 :(得分:0)

我假设您从下拉列表中选择了tablename之后在某处调用了readItemsFromDatabaseforTable:方法,并且上面没有显示该代码。致电后,您需要拨打以下电话:

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

(flashScrollIndicators不是必需的,但添加了一个很好的视觉线索,表重新加载)

作为旁注,您的itemsList数组将泄漏,因为您每次都要重新分配它。您应该在其他地方分配,然后在readItemsFromDatabaseforTable:调用(而不是分配)中分配:

[itemsList removeAllObjects];

您正在创建一个全新的委托对象,我不明白,您似乎也在除了tableview之外的其他一些对象上调用reloadData。您应该像这样更新dropDownCellSelected:方法:

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

答案 2 :(得分:0)

你重装了UITableView的数据吗? 像

[self.tableView reloadData]

答案 3 :(得分:0)

我解决了我的问题,因为更改了代码 - (void)dropDownCellSelected:(NSInteger)returnIndex { 作为休耕

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