我在发布一些数据后尝试重新加载我的 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");
}
}
我应该怎么做才能用新添加的数据重新加载表?
谢谢。