【问题标题】:JSONKit with UITableview带有 UITableview 的 JSONKit
【发布时间】:2012-08-18 11:55:16
【问题描述】:

我在解析表格视图时遇到了 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 吗?

【问题讨论】:

  • 你能解释一下你的问题是什么吗?如果该代码没有崩溃,resultsDictionary 必须是 NSArray,因为您在其上调用 NSArray 方法。

标签: ios uitableview jsonkit


【解决方案1】:

不确定这是否是问题,但您似乎缺少一行来初始化您的 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;
            }
        }
    }

    ...


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多