由于未捕获的异常而终止应用程序' NSInvalidArgumentException 4

时间:2014-11-11 04:15:44

标签: ios

我是ios的新手。我正在制作第一个应用程序,其中我从服务器调用数据并显示在表视图单元格中,但它提供了一个异常,提到了。数据正确,但当我填充我的表视图单元格异常时显示。提前的thanx是我的代码

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *tableviewidentifier = @"cell";
        UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:tableviewidentifier];
        if(cell==nil){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableviewidentifier];
        }
        NSMutableArray *jsonresultarr=[NSMutableArray new];

        NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://magmasysdev.com/ddc/getAllCompanies.php"]];  // this is your request url

        [request setHTTPMethod:@"GET"];

        [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];

        NSError *err;
        NSURLResponse *response;
        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

        NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];     // here parsing the array

        NSArray *firstArry=[[jsonArray objectAtIndex:1]objectForKey:@"company_data"];

        for (int i=0; i<[firstArry count]; i++)
        {
            NSMutableDictionary *getjsonresponse= [firstArry objectAtIndex:i];

            NSString *Company_Id=[getjsonresponse objectForKey:@"Company_Id"];
            NSString *Company_Name=[getjsonresponse objectForKey:@"Company_Name"];
            NSString *Company_Address=[getjsonresponse objectForKey:@"Company_Address"];
            NSString *Company_Email=[getjsonresponse objectForKey:@"Company_Email"];
            NSString *Company_Tel_Num=[getjsonresponse objectForKey:@"Company_Tel_Num"];
            NSString *Company_Website=[getjsonresponse objectForKey:@"Company_Website"];
            NSString *Company_Fax_Num=[getjsonresponse objectForKey:@"Company_Fax_Num"];
            [jsonresultarr addObject:Company_Id];
            [jsonresultarr addObject:Company_Name];
            [jsonresultarr addObject:Company_Address];
            [jsonresultarr addObject:Company_Email];
            [jsonresultarr addObject:Company_Tel_Num];
            [jsonresultarr addObject:Company_Website];
            [jsonresultarr addObject:Company_Fax_Num];
            cell.textLabel.text=[[jsonresultarr objectAtIndex:indexPath.row]objectForKey:@"Company_Id"]; // getting exception in this line..

        }




            return cell;
    }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // Return the number of rows in the section.
    NSMutableArray *jsonresultarr=[NSMutableArray new];

    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://magmasysdev.com/ddc/getAllCompanies.php"]];  // this is your request url

    [request setHTTPMethod:@"GET"];

    [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];

    NSError *err;
    NSURLResponse *response;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];     // here parsing the array

    NSArray *firstArry=[[jsonArray objectAtIndex:1]objectForKey:@"company_data"];

    for (int i=0; i<[firstArry count]; i++)
    {
        NSMutableDictionary *getjsonresponse= [firstArry objectAtIndex:i];

        NSString *Company_Id=[getjsonresponse objectForKey:@"Company_Id"];
        NSString *Company_Name=[getjsonresponse objectForKey:@"Company_Name"];
        NSString *Company_Address=[getjsonresponse objectForKey:@"Company_Address"];
        NSString *Company_Email=[getjsonresponse objectForKey:@"Company_Email"];
        NSString *Company_Tel_Num=[getjsonresponse objectForKey:@"Company_Tel_Num"];
        NSString *Company_Website=[getjsonresponse objectForKey:@"Company_Website"];
        NSString *Company_Fax_Num=[getjsonresponse objectForKey:@"Company_Fax_Num"];
        [jsonresultarr addObject:Company_Id];
        [jsonresultarr addObject:Company_Name];
        [jsonresultarr addObject:Company_Address];
        [jsonresultarr addObject:Company_Email];
        [jsonresultarr addObject:Company_Tel_Num];
        [jsonresultarr addObject:Company_Website];
        [jsonresultarr addObject:Company_Fax_Num];



}
    return jsonresultarr.count;
}

1 个答案:

答案 0 :(得分:1)

您遵循的方法不是一个好方法。对于每个单元,它调用Web服务以影响性能。

 cell.textLabel.text=[[jsonresultarr objectAtIndex:indexPath.row]objectForKey:@"Company_Id"];

这里jsonresultarr有一个NSString的对象而不是NSDictionary,所以更改为如下所示

cell.textLabel.text=[jsonresultarr objectAtIndex:indexPath.row];