2012-01-08 3 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의 코드. 이미 나는이 새로운 해요 말했듯이

#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 

, 그래서 어떤 도움은 매우 매우 환영받을 것이다 :이 엑스 코드에서 내 코드

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

?> 

입니다 :

이 내 PHP 코드입니다 ! 나는이 문제를 며칠 동안 파악하려고 노력하고 있지만 정확한 답이나 해결책을 찾을 수없는 것 같습니다!

미리 감사드립니다. 나는 당신의 데이터베이스에서오고 개체 중 하나 같은데요

답변

2

가 DB에 NULL입니다, 제대로 JSON에서 null로 번역되고 올바르게 TouchJSON에 NSNull로 번역된다. 그런 다음 사전에서 꺼내서 UILabel 텍스트로 설정합니다.

tableView:cellForRowAtIndexPath:에 수표를 추가하여 실제로 객체가 NSString인지 확인해야합니다. 아마 같은 :

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

, 왜 당신이 행 테이블 뷰 셀을 요청 할 때마다 정렬된다? 데이터를 가져올 때 한 번만 정렬해야합니다 (예 : viewDidLoad).

관련 문제