2012-01-08 66 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