重新加载TaUITableViewbleView

时间:2011-05-21 16:15:00

标签: iphone xcode uitableview

我遇到了问题,我制作了一个TabBAr应用程序(带导航栏),条形栏是存储在数组中的优惠列表。 我的问题是,如果我更改ViewController并将对象添加到数组,当我回到UITableView时,它不会被重新加载... 这是班级:

-

 (void)viewDidLoad {
    [super viewDidLoad];

    [self readArgFromDatabaseSottoArgomenti];
    [self VisualizzaPreferiti];

}

- (void)viewWillAppear:(BOOL)animated {
    [self.tableView reloadData];
}

-(void) readArgFromDatabaseSottoArgomenti {

    databasePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"ARGOMENTI.sqlite"];

    sqlite3 *databaseDesc;
    // Init the argoments Array
    arraySottoArgomenti = [[NSMutableArray alloc] init];

    // Open the database from the users filessytem
    if(sqlite3_open([databasePath UTF8String], &databaseDesc) == SQLITE_OK) {
        // Setup the SQL Statement and compile it for faster access
        // const char *sqlStatement = "select * from DESCRIZIONE ";
        const char *sqlStatement = [[NSString stringWithFormat:@"SELECT * from DESCRIZIONE ORDER BY id"] UTF8String];
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(databaseDesc, 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 *aID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
                NSString *aIDArgomento = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                NSString *aDescrizione = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
                NSString *aTesto = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];

                // Create a new argoments object with the data from the database
                ContenutoObjectDescrizione *contenutoSottoArgomenti = [[ContenutoObjectDescrizione alloc] initWithName:aID idArgomento:aIDArgomento descrizione:aDescrizione testo:aTesto];
                [arraySottoArgomenti addObject:contenutoSottoArgomenti];

                [contenutoSottoArgomenti release];
            }
        }


        // Release the compiled statement from memory
        sqlite3_finalize(compiledStatement);

    }
    sqlite3_close(databaseDesc);

}

- (void) VisualizzaPreferiti {

    int i;

    NSUserDefaults *userPref = [NSUserDefaults standardUserDefaults];
    array = [userPref objectForKey:@"array"];


    NSLog(@"Retain Count %d Numero ID Array %d",[array retainCount],[array count]);



    NSMutableArray *arrayOggettoPreferito;
    arrayOggettoPreferito = [[NSMutableArray alloc] init];

    ContenutoObjectDescrizione *oggetto = [[ContenutoObjectDescrizione alloc] init];

    for (oggetto in arraySottoArgomenti) {
        for (i=0; i<[array count]; i++) {

            if ([[array objectAtIndex:i] intValue] == [oggetto.id intValue]) {
                [arrayOggettoPreferito addObject:oggetto];

                NSLog(@"ID %@ IDMateria %@ Titolo %@",oggetto.id,oggetto.idArgomento,oggetto.descrizione);
            }
        }
    }   

    listaPref = arrayOggettoPreferito;

    arrayOggettoPreferito=nil;
    [arrayOggettoPreferito release];
    [oggetto release];  

}

#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 [listaPref 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];
    }

    ContenutoObjectDescrizione *oggettoCercato = [[ContenutoObjectDescrizione alloc] init];
    oggettoCercato = [listaPref objectAtIndex:[indexPath row]];

    cell.textLabel.text = oggettoCercato.descrizione;
    NSLog(@"%@",oggettoCercato.descrizione);


    return cell;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    TestoViewController *testoViewController = [[TestoViewController alloc] initWithNibName:@"TestoView" bundle:nil];
    [self.navigationController pushViewController:testoViewController animated:YES];

    ContenutoObjectDescrizione *oggettoCercato = [[ContenutoObjectDescrizione alloc] init];
    oggettoCercato = [listaPref objectAtIndex:[indexPath row]];

    testoViewController.idPreferito = oggettoCercato.id;

    testoViewController.title = oggettoCercato.descrizione;

    NSString *descrizioneWeb = oggettoCercato.testo;

    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSURL *baseURL = [NSURL fileURLWithPath:path];

    [testoViewController.vistaWeb loadHTMLString:descrizioneWeb baseURL:baseURL];
    [testoViewController release];


}

#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 {
    [super dealloc];
}

1 个答案:

答案 0 :(得分:1)

除非您更新数据源,否则简单地调用reloadData并不会使其执行任何操作。在viewWillAppear,您需要在致电VisualizzaPreferiti之前再次致电reloadData