重新加载 TaUITableViewbleView

发布于 2024-11-08 17:14:59 字数 5874 浏览 0 评论 0原文

我有一个问题,我制作了一个 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];
}

I have a problem, I make a TabBAr application (with navigationbar), the bar bar is a list of favorits stored in an array.
My problem is that if I change ViewController and add object to array, when I come back to UITableView it isn't reloaded...
This is the class:

-

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

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

Simply calling reloadData doesn't make it do anything unless you update your datasource. In viewWillAppear, you will need to call VisualizzaPreferiti again before you call reloadData.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文