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 是一本字典,我也尝试过使用数组,但总是让应用程序在滚动 tableview 后崩溃!请有任何提示,以正确解析 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 回答