[英]Terminating app due to uncaught exception 'NSInvalidArgumentException 4
I am new to ios. 我是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 下面上面的预先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;
}
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 . 对于每个单元,它都会调用Web服务,从而影响性能。
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 在这里jsonresultarr
将NSString的对象不是NSDictionary,因此更改为如下所示
cell.textLabel.text=[jsonresultarr objectAtIndex:indexPath.row];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.