2011-05-21 2 views
0

У меня проблема, я делаю приложение TabBAr (с навигационной панелью), панель - это список фаворитов, хранящихся в массиве. Моя проблема заключается в том, что если я изменю ViewController и добавить объект в массив, когда я вернусь к UITableView это не перезагружается ... Это класс:Reload TaUITableViewbleView

-

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

retainCount бесполезен. Не называйте это. – bbum

ответ

1

Просто позвонив reloadData ничего не делает, если вы не обновите свой источник данных. В viewWillAppear вам нужно будет позвонить VisualizzaPreferiti еще до того, как вы позвоните reloadData.

+0

Я также стараюсь это, но ничего. - (void) viewWillAppear { \t \t [self VisualizzaPreferiti]; \t [self.tableView reloadData]; } – kikko088

+0

Ну, это может произойти из-за того, что вы пытаетесь получить доступ к массиву в пользовательских значениях по умолчанию. Это не лучшее место для хранения данных. Попробуйте сохранить plist или что-то в каталоге документов. –

+0

Один вопрос, я вижу, что если я делаю простой NSLog (@ "test"); in viewWillAppear ничего не отображается на консоли ... Это как если бы функция не вызывала ... – kikko088