objective-c - 发送 POST 后重新加载 UITableView

标签 objective-c ios

我在发布一些数据后尝试重新加载我的 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/

相关文章:

c++ - 在 C++ 程序中从 NSNotification Server 获取通知

iphone - 点击时如何让 UIButton 执行我的类方法?

ios - WatchKit在handleWatchKitExtensionRequest中的 block 内返回Reply():

ios - 是否可以通过附加 NSString 的一部分来指定变量名?

ios - 连接2点drawRect?

ios - 在 post 方法中上传音频和附加参数

iOs 8、核心运动和骑行事件

ios - 为什么我的 subview 没有添加到我的 ScrollView 中?

ios - xcodebuild 构建命令在 Xcode 11.5 中失败

iphone - 申请因不遵循 iOS 数据存储指南而被拒绝