2012-01-08 5 views
1

こんにちは、私はCocoa Developmentを初めて利用しています。私が間違っていたことを理解しようとしています。私はtouchJSONを使用してXcodeのmySQLデータベースでtableViewを塗りつぶした(tutorial)を追跡しました。touchJSONはNSInvalidExceptionを返しますNSNull isEqualtoString:

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

これはPHPコード(およびデータベース)とは何かを持っている場合、私は本当に知らない:私はアプリケーションを実行すると、すべてが正常に動作しているが、私はのtableViewを下にスクロールするとき、私はNSInvalidExeptionエラーを取得しますまたは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に翻訳されている私はあなたのデータベースから来るのオブジェクトの1つを推測しています。次に、それを辞書から取り出してUILabelのテキストとして設定します。

tableView:cellForRowAtIndexPath:にチェックを入れて、オブジェクトが実際にNSStringであることを確認する必要があります。おそらくのようなもの:

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

、なぜあなたは、行テーブルビューは、セルを要求するたびにソートされていますか?あなたがデータを取得すると、つまり、あなたのケースではviewDidLoadというように、それらを一度並べ替えるだけでよいでしょう。

関連する問題