我正在创建一个应用程序,我想从PhpMyAdmin数据库获取数据到我的UITableView
。
有什么好的教程吗?请不要任何聊天应用教程,我只是想在tableview中显示我的表。
我的表有5个字段(id, fname, lname, email, address)。
谢谢
最好的处理方法是为你的数据库创建一个JSON API,然后让你的iOS应用解析JSON数据。
下面是如何使用PHP从MySQL创建JSON输出的示例。
当你有你的JSON输出时,你应该使用NSURLConnection
和NSJSONSerialization
从应用程序下载它,像这样:
NSURL* url = [NSURL urlWithString:@"http://yourapi.url/here"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *response = nil;
NSError **error = nil;
NSData *data = [[NSData alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:error]];
NSInteger httpStatus = [((NSHTTPURLResponse *)response) statusCode];
NSLog(@"responsecode:%d", httpStatus);
NSDictionary *parsedJSON = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&errorInfo];
JSON的内容将放在parsedJson
字典中。这只是一个使用NSURLConnection
同步请求的快速示例。如果你想要一个异步解决方案,你应该阅读NSURLConnection
的文档。
要显示UITableView
中的数据,您应该查看本教程。