Reputation: 724
I am new to ios. I am making first app in which i am calling data from server and showing in table view cell but it is giving an exception which is mentioned. data is coming correctly but when i populate my table view cell exception is shown. thanx in advance above below is my code
- (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;
}
Upvotes: 0
Views: 51
Reputation: 15335
the approach, which you've followed is not a good way . For every cell, it calls the web services so that it effects the performance .
cell.textLabel.text=[[jsonresultarr objectAtIndex:indexPath.row]objectForKey:@"Company_Id"];
Here jsonresultarr
has hols the object of NSString not an NSDictionary , So change to like the below
cell.textLabel.text=[jsonresultarr objectAtIndex:indexPath.row];
Upvotes: 1