0

テーブルビューの解析時に JsonKit で問題が発生しています。基本的には次のことを試しました。

- (void)viewDidLoad {

 [super viewDidLoad];

     //1 check if is it the login plist
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
     NSString *path = [documentsDirectory stringByAppendingPathComponent:@"login.plist"];
     NSLog(@"path='%@'",path);


     NSFileManager *nfm = [[NSFileManager alloc] init];
     if([nfm fileExistsAtPath:path])
     {
         //Start login process
         NSArray* dict = [[NSArray alloc] initWithContentsOfFile:path];
         NSLog(@"content from the login.plist :%@",dict);
         NSString *emailstring = [dict objectAtIndex:0];
         NSString *passstring = [dict objectAtIndex:1];

         // Create the URL from a string.
         NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.mywebsite.com/API/expert?format=json&email=%@&password=%@&start=1&end=10",emailstring,passstring]];

         // Creating a request object using the URL.
         NSURLRequest *request = [NSURLRequest requestWithURL:url];

         // Prepare for the response back from the server    
         NSHTTPURLResponse *response = nil;
         NSError *error = nil;

         // Send a synchronous request to the server (i.e. sit and wait for the response)
         NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
         NSLog(@"Repoonse from web:%@", response);

         // Check if an error occurred    
         if (error != nil) {
             NSLog(@"%@", [error localizedDescription]);

         }

         // View the data returned - should be ready for parsing.
         resultsDictionary = [responseData objectFromJSONData];
         NSLog(@"ResultsDictionary:%@", resultsDictionary);

         numberexp = [resultsDictionary count];
         printf("Results dictionary online %d\n", numberexp);

     }

   }



// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

    CellExpOnline *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"CellExpOnline" owner:nil options:nil];

        for (UIView *view in views) {
            if([view isKindOfClass:[UITableViewCell class]])
            {
                cell = (CellExpOnline*)view;
            }
        }
    }

    cell.textLabel.text = [[resultsDictionary objectAtIndex:indexPath.row] valueForKey:@"titulo"];
    cell.detailTextLabel.text = [[resultsDictionary objectAtIndex:indexPath.row] valueForKey:@"description"];


    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;


}

resultsDictionary は辞書です。配列でも試しましたが、常にテーブルビューをスクロールした後にアプリがクラッシュします!! UITableview の JSONKit を正しく解析するためのヒントを教えてください。

4

1 に答える 1

0

これが問題かどうかはわかりませんが、UITableViewCell を初期化するための行が欠落しているようです:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    CellExpOnline *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        // -----------------------------------------------------------
        // Aren't you missing something like this here:
        // (assuming you're not using Automatic Rreference Counting)
        // -----------------------------------------------------------

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];

        // -----------------------------------------------------------

        NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"CellExpOnline" owner:nil options:nil];

        for (UIView *view in views) {
            if([view isKindOfClass:[UITableViewCell class]])
            {
                cell = (CellExpOnline*)view;
            }
        }
    }

    ...


}
于 2012-08-23T04:12:40.017 に答える