我刚刚制作了一个项目,并且我使用了此网站的代码:http://codewithchris.com/iphone-app-connect-to-mysql-database/
问题当然是ViewController.m。它中的代码没有执行,我已经尝试将NSLog(@"Test With NSLog");
放在ViewController.m中,但没有任何反应。来自" site_url"的JSON东西一切都很好。
以下是所有代码:
ViewController.h
#import <UIKit/UIKit.h>
#import "HomeModel.h"
@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, HomeModelProtocol>
@property (weak, nonatomic) IBOutlet UITableView *listTableView;
@end
ViewController.m
#import "ViewController.h"
#import "Location.h"
@interface ViewController ()
{
HomeModel *_homeModel;
NSArray *_feedItems;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Set this view controller object as the delegate and data source for the table view
self.listTableView.delegate = self;
self.listTableView.dataSource = self;
// Create array object and assign it to _feedItems variable
_feedItems = [[NSArray alloc] init];
// Create new HomeModel object and assign it to _homeModel variable
_homeModel = [[HomeModel alloc] init];
// Set this view controller object as the delegate for the home model object
_homeModel.delegate = self;
// Call the download items method of the home model object
[_homeModel downloadItems];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)itemsDownloaded:(NSArray *)items
{
// This delegate method will get called when the items are finished downloading
// Set the downloaded items to the array
_feedItems = items;
// Reload the table view
[self.listTableView reloadData];
}
#pragma mark Table View Delegate Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of feed items (initially 0)
return _feedItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Retrieve cell
NSString *cellIdentifier = @"BasicCell";
UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// Get the location to be shown
Location *item = _feedItems[indexPath.row];
// Get references to labels of cell
myCell.textLabel.text = item.username;
return myCell;
}
@end
HomeModel.h
#import <Foundation/Foundation.h>
@protocol HomeModelProtocol <NSObject>
- (void)itemsDownloaded:(NSArray *)items;
@end
@interface HomeModel : NSObject <NSURLConnectionDataDelegate>
@property (nonatomic, weak) id<HomeModelProtocol> delegate;
- (void)downloadItems;
@end
HomeModel.m
#import "HomeModel.h"
#import "Location.h"
@interface HomeModel()
{
NSMutableData *_downloadedData;
}
@end
@implementation HomeModel
- (void)downloadItems
{
// Download the json file
NSURL *jsonFileUrl = [NSURL URLWithString:@"site_url"];
// 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.username = jsonElement[@"Username"];
newLocation.email = jsonElement[@"Email"];
newLocation.money = jsonElement[@"money"];
// 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
Location.h
#import <Foundation/Foundation.h>
@interface Location : NSObject
@property (nonatomic, strong) NSString *username;
@property (nonatomic, strong) NSString *email;
@property (nonatomic, strong) NSString *money;
@end
Location.m
#import "Location.h"
@implementation Location
@end