0

データを投稿した後、UITableView をリロードしようとしています。コードは次のとおりです。

-(IBAction)carregaLista{

NSError *err = nil;
NSURLResponse *response = nil;

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

NSString *theURL = [NSString stringWithFormat:@"http://localhost/tests/json/"];
NSURL *URL = [NSURL URLWithString:theURL];
[request setURL:URL];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setTimeoutInterval:30];

NSData *jsonData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

resultsDictionary = [[NSDictionary alloc] init];

resultsDictionary = [jsonData objectFromJSONData];
//NSLog(@"my dictionary = %@", resultsDictionary);


self.billsArray = [[NSArray alloc] init];
self.billsArray = [resultsDictionary objectForKey:@"bills"];

// Add values to its array

double sum = [[self.billsArray valueForKeyPath:@"@sum.value"] doubleValue];

NSString *totalString = [NSString stringWithFormat:@"%0.2f", sum];
self.total.text = totalString;

// Custom table cell

tabela.rowHeight = 100;
[self.tabela reloadData];
NSLog(@"Chamou carregaLista");

}

次に、AddViewController で:

-(IBAction)enviaCadastro:(id)sender{

NSLog(@"Nome: %@, Valor: %@", self.nome.text, self.valor.text);

NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost/tests/json/add.php"]];
[request setHTTPMethod:@"POST"];

NSString *post =[[NSString alloc] initWithFormat:@"nome=%@&valor=%@", self.nome.text, self.valor.text];
[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];

NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

float respCode = [responseString floatValue];
if(respCode < 400) {
    [self dismissModalViewControllerAnimated:YES];
    ViewController *firstView = [[ViewController alloc] init];
    [firstView carregaLista];
}
else {
    NSLog(@"NAO DEU CERTO");
}


}

新しく追加されたデータでテーブルをリロードするにはどうすればよいですか?

ありがとう。

4

3 に答える 3

1

これ...

ViewController *firstView = [[ViewController alloc] init];

...既存のインスタンスとは異なる新しいView Controllerを作成し、画面に表示されるビューを持つものではありません。そのデータ プロパティを更新していて、その更新がビューに表示されることを期待している可能性があります。アプリの一部であるcarregaListaを呼び出す必要があると思います。ViewController

于 2012-08-02T14:48:03.337 に答える
0

これが同じケースかどうかはわかりませんが、UIPickerView -reloadAllComponentsメソッドで同様の問題が発生しました。行が画面から消えた場合、更新されて戻ってくることに気付きました。以前の方法で期待していたことのようなものです。

それを更新する方法は、メソッドを使用して NSMutableArray にあったものを削除し、-removeAllObjectsすぐに then を呼び出す-addObject:ことでした-reloadAllComponents。似たようなことをして、必要な結果を得ることができると感じています。

于 2012-08-02T14:55:44.813 に答える
0

考え出した。の中で呼んcarregaListaでいviewDidLoadました。中に入れるだけでviewWillAppearうまくいきました。みんなに感謝します。

于 2012-08-02T18:03:37.967 に答える