下午好,
我正试图使用TableView控制器来显示MySQL数据库中的X个项目,但目前我有点不知所措。
我有另一个项目,可以使用JSON输出数据显示数据,但我不知道如何将该代码添加到我的项目中,以便显示数据库中的数据。这是我使用的代码:
@implementation HomeModel
- (void)downloadItems
{
// Download the json file
NSURL *jsonFileUrl = [NSURL URLWithString:@"http://website.com/service.php"];
// Create the request
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];
// Create the NSURLConnection
[NSURLConnection connectionWithRequest:urlRequest delegate:self];
}
#pragma mark NSURLConnectionDataProtocol Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// Initialize the data object
_downloadedData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the newly downloaded data
[_downloadedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Create an array to store the locations
NSMutableArray *_locations = [[NSMutableArray alloc] init];
// Parse the JSON that came in
NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingAllowFragments error:&error];
// Loop through Json objects, create question objects and add them to our questions array
for (int i = 0; i < jsonArray.count; i++)
{
NSDictionary *jsonElement = jsonArray[i];
// Create a new location object and set its props to JsonElement properties
Location *newLocation = [[Location alloc] init];
newLocation.name = jsonElement[@"user"];
newLocation.address = jsonElement[@"imagen"];
newLocation.latitude = jsonElement[@"date"];
// Add this question to the locations array
[_locations addObject:newLocation];
}
// Ready to notify delegate that data is ready and pass back items
if (self.delegate)
{
[self.delegate itemsDownloaded:_locations];
}
}
@end
目前这是我的代码:
TableViewController.m
#import "CarTableViewController.h"
#import "CarTableViewCell.h"
#import "CarTableViewController.h"
#import "CarDetailViewController.h"
@implementation CarTableViewController
@synthesize carMakes = _carMakes;
@synthesize carModels = _carModels;
@synthesize carImages = _carImages;
- (void)viewDidLoad
{
[super viewDidLoad];
self.carMakes = [[NSArray alloc]
initWithObjects:@"Chevy",
@"BMW",
@"Toyota",
@"Volvo",
@"Smart", nil];
self.carModels = [[NSArray alloc]
initWithObjects:@"Volt",
@"Mini",
@"Venza",
@"S60",
@"Fortwo", nil];
self.carImages = [[NSArray alloc]
initWithObjects:@"chevy_volt.jpg",
@"mini_clubman.jpg",
@"toyota_venza.jpg",
@"volvo_s60.jpg",
@"smart_fortwo.jpg", nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.carModels count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"carTableCell";
CarTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CarTableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.makeLabel.text = [self.carMakes
objectAtIndex: [indexPath row]];
cell.modelLabel.text = [self.carModels
objectAtIndex:[indexPath row]];
UIImage *carPhoto = [UIImage imageNamed:
[self.carImages objectAtIndex: [indexPath row]]];
cell.carImage.image = carPhoto;
return cell;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"ShowCarDetails"])
{
CarDetailViewController *detailViewController =
[segue destinationViewController];
NSIndexPath *myIndexPath = [self.tableView
indexPathForSelectedRow];
detailViewController.carDetailModel = [[NSArray alloc]
initWithObjects: [self.carMakes
objectAtIndex:[myIndexPath row]],
[self.carModels objectAtIndex:[myIndexPath row]],
[self.carImages objectAtIndex:[myIndexPath row]],
nil];
}
}
@end
TableViewController.h
#import <UIKit/UIKit.h>
@interface CarTableViewController : UITableViewController
@property (nonatomic, strong) NSArray *carImages;
@property (nonatomic, strong) NSArray *carMakes;
@property (nonatomic, strong) NSArray *carModels;
@end
如何将第一个代码(来自数据库的结果)添加到其他项目中?我现在迷路了,如果你能给我一些关于这个问题的线索,我将不胜感激,因为我想像在第二个项目中那样编辑故事板中的信息。
提前谢谢。
谨致问候。
提前谢谢。
这是因为您不能直接从iphone连接到数据库。一般来说,您有一个web服务(也称为API)来公开您的数据(例如JSON格式)。因此,您必须用PHP、Ruby或Node构建一个API,以公开数据库的数据并访问它
编辑:
我看到的是,在你的HomeModel中,你可以设置一个委托,这个委托有一个函数itemsDownloaded
,你可以在这里传递从API获得的数据。
因此,您希望您的CarTableViewController
成为接收这些数据的代理。
首先,您必须在TableViewController.h add:中有一个对象HomeModel
@property (strong, nonatomic) HomeModel *homeModel;
现在,您必须调用从服务器获取数据的代码。类似的东西
- (void)viewDidLoad
{
[super viewDidLoad];
homeModel = [HomeModel new];
homeModel.delegate = self;
[homeModel downloadItems];
}
最后,您必须在CarTableViewController
中实现itemsDownloaded
。这样做:
- (void)itemsDownloaded:(NSArray *)items
{
self.carMakes = items; // Problem here
[self.tableView reloadData];
}
但问题是HomeModel
解析的内容与carMakes
中的内容不同,您必须调整HomeModel
中的Loop through Json objects
。你可能想试试AFNetworking,这是一个比NSURLConnection更容易使用的库。