2012-01-08 62 views
1

你好,我是新的可可开发,我试图找出我做错了什么。我跟着(tutorial)使用touchJSON在Xcode中用mySQL数据库填充tableView。当我运行应用程序一切正常,但是当我向下滚动的tableView我得到一个错误NSInvalidExeptiontouchJSON给NSInvalidException NSNull isEqualtoString:

Terminating app due to uncaught exception 'NSInvalidArgumentException', 
    reason: '-[NSNull isEqualToString:]: unrecognized selector sent to 
     instance 0x1469cd8' 

我真的不知道,如果这事做与PHP代码(和数据库)或Xcode中的代码。

这是我的PHP代码:

<?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).'}'; 

?> 

这是我的代码的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 

正如我已经告诉我是新来的这一点,所以任何帮助将是非常非常受欢迎!我试图找出这个问题几天,但我似乎无法找到一个准确的答案或解决方案!

非常感谢您的努力提前!

回答

2

我猜数据库中的一个对象是DB中的NULL,在JSON中正确转换为null,并且在TouchJSON中正确转换为NSNull。然后,您将它从字典中取出并将其设置为UILabel的文本。

您应该在您的tableView:cellForRowAtIndexPath:中添加支票以检查对象实际上是NSString s。可能是这样的:

id Naam = [dict objectForKey:@"Naam"]; 
if ([Naam isKindOfClass:[NSString class]]) { 
    cell.Naam.text = Naam; 
} else { 
    cell.Naam.text = @""; 
} 

此外,为什么你排序行每次表视图要求单元格?当你得到数据的时候,你应该把它们排序一次 - 例如你的案例中的viewDidLoad