我在发布一些数据后尝试重新加载我的 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");
}
}
我应该怎样做才能使用新添加的数据重新加载表格?
谢谢。
最佳答案
这个...
ViewController *firstView = [[ViewController alloc] init];
...创建一个新 View Controller ,它与您已有的任何实例不同,并且不是具有屏幕上可见 View 的实例。您可能正在更新其数据属性并期望更新出现在您的 View 中。我相信您需要在已经属于您的应用一部分的 ViewController
上调用 carregaLista
。
关于objective-c - 发送 POST 后重新加载 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11779048/