Ios 由于未捕获异常而终止应用程序';NSInvalidArgumentException 4
我是ios新手。我正在制作第一个应用程序,在其中我从服务器调用数据并显示在表视图单元格中,但它给出了一个提到的异常。数据正确,但当我填充表格视图时,会显示单元格异常。下面是我的代码Ios 由于未捕获异常而终止应用程序';NSInvalidArgumentException 4,ios,Ios,我是ios新手。我正在制作第一个应用程序,在其中我从服务器调用数据并显示在表视图单元格中,但它给出了一个提到的异常。数据正确,但当我填充表格视图时,会显示单元格异常。下面是我的代码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *tableviewidentifier = @"c
- (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;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*tableviewidentifier=@“单元格”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:tableviewidentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:tableviewidentifier];
}
NSMUTABLEARRY*JSONResultar=[NSMUTABLEARRY new];
NSMutableURLRequest*请求=[NSMutableUrlRequestRequestWithURL:[NSURL URLWithString:@]http://magmasysdev.com/ddc/getAllCompanies.php“]];//这是您的请求url
[请求设置HttpMethod:@“获取”];
[request setValue:@“application/json;charset=UTF-8”forHTTPHeaderField:@“content type”];
n错误*错误;
NSURLResponse*响应;
NSData*responseData=[NSURLConnection sendSynchronousRequest:request ReturnInResponse:&响应错误:&错误];
NSArray*jsonArray=[NSJSONSerialization JSONObjectWithData:responseData选项:NSJSONReadingMutableContainers错误:&err];//此处解析数组
NSArray*firstArry=[[jsonArray objectAtIndex:1]objectForKey:@“公司数据”];
对于(inti=0;i您所遵循的方法不是一个好方法。对于每个单元,它调用web服务以影响性能
cell.textLabel.text=[[jsonresultarr objectAtIndex:indexPath.row]objectForKey:@"Company_Id"];
这里的jsonresultar
包含了NSString的对象,而不是NSDictionary,因此更改为如下所示
cell.textLabel.text=[jsonresultarr objectAtIndex:indexPath.row];
现在出现此错误的原因是“NSRangeException:”***-[\uu NSArrayM objectAtIndex:]:索引7超出范围[0..6]请正确地为代理提供数据源,最好请阅读UITableView.huf的基础知识,请不要这样做,将其作为Web服务的单独函数,并使用实例变量来保存结果值OK。很好。实际上,在我检查时,但我现在可以更改:)如何在单元格中为indexpath处的行调用该函数??