Hello I am new to Cocoa Development, and I'm trying to figure out what I did wrong. I followed a (tutorial) that uses touchJSON to fill a tableView with a mySQL database in Xcode. When I run the application everything is working fine, but when I scroll down the tableView I get an NSInvalidExeption
error:
Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[NSNull isEqualToString:]: unrecognized selector sent to
instance 0x1469cd8'
I don't really know if this has something to do with the php code (and database) or the code in Xcode.
This is my php code:
<?php
$link = mysql_pconnect("localhost", "root", "root") or die("Could not connect");
mysql_select_db("PartyON") or die("Could not select database");
$arr = array();
$rs = mysql_query("SELECT id, Maand, Naam, Locatie, Plaats FROM tblWebData");
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}
echo '{"tblWebData":'.json_encode($arr).'}';
?>
This is my code from Xcode:
#import "GentDataView.h"
#import "CJSONDeserializer.h"
#import "GentDetailCell.h"
@implementation GentDataView
@synthesize rows, tableview;
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://localhost:8888/example3.php"]; //URL Modification
NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url]; // Pulls the URL
// NSLog(jsonreturn); // Look at the console and you can see what the restults are
NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
NSError *error = nil;
// In "real" code you should surround this with try and catch
NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
if (dict)
{
rows = [dict objectForKey:@"tblWebData"];
}
NSLog(@"Array: %@",rows);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [rows count];
}
// Customize the appearance of table view cells.
- (GentDetailCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
GentDetailCell *cell = (GentDetailCell *) [tableview dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[GentDetailCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell.
NSSortDescriptor *sorteerDiscriptor = [[NSSortDescriptor alloc] initWithKey:@"id" ascending:NO];
rows = [rows sortedArrayUsingDescriptors:[NSArray arrayWithObject:sorteerDiscriptor]];
NSDictionary *dict = [rows objectAtIndex: indexPath.row];
cell.Naam.text = [dict objectForKey:@"Naam"];
cell.Plaats.text = [dict objectForKey:@"Plaats"];
cell.Maand.text = [dict objectForKey:@"Maand"];
cell.Locatie.text = [dict objectForKey:@"Locatie"];
cell.imageView.image = [NSURL URLWithString:@"http://www.iconarchive.com/show/flags-icons-by-iconscity/belgium-icon.html"];
//cell.textLabel.text = [dict objectForKey:@"post_title"];
//cell.detailTextLabel.text = [dict objectForKey:@"post_content"];
//tableView.backgroundColor = [UIColor cyanColor];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 125;
}
@end
As I already told I'm new to this, so any help would be very very welcome! I'm trying to figure out this issue for days now but I can't seem to find an accurate answer or a solution!
Thank you very much for your efforts in advance!
I'm guessing one of the objects that's coming from your database is
NULL
in the DB, being correctly translated into anull
in the JSON and being correctly translated into anNSNull
in TouchJSON. Then you're grabbing it out of the dictionary and setting it as the text of aUILabel
.You should add checks in your
tableView:cellForRowAtIndexPath:
to check the objects are actuallyNSString
s. Probably something like:Also, why are you sorting the rows each time the table view asks for a cell? You should probably just sort them once, when you get the data - i.e. in
viewDidLoad
in your case.