我正在解析一个json url并加载UITableview,我点击一行并导航到其他视图。当我 我将返回主视图并再次单击一行,我有异常,请帮助。在viewWillAppear中分配用于填充tableview的所有数组,并添加数组元素
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
locArr=[[NSMutableArray alloc]init];
descArr=[[NSMutableArray alloc]init];
contArr=[[NSMutableArray alloc]init];
infoArr=[[NSMutableArray alloc]init];
addArr=[[NSMutableArray alloc]init];
typeArr=[[NSMutableArray alloc]init];
bigimgArr=[[NSMutableArray alloc]init];
imgArr=[[NSMutableArray alloc]init];
nameArr=[[NSMutableArray alloc]init];
catArr=[[NSMutableArray alloc]init];
_res_lbl.hidden=YES;
self.mytable_view.hidden=YES;
TopPaidAppsFeed =[NSString stringWithFormat:@"%@/ami/activities.php?type=%@",urlconstant,self.type];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:TopPaidAppsFeed]];
self.appListFeedConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
NSAssert(self.appListFeedConnection != nil, @"Failure to create URL connection.");
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"LazyTableCell";
SimplecellCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[SimplecellCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSUInteger nodeCount = [self.entries count];
if (nodeCount > 0)
{
// NSLog(@"NodeCount================%d",nodeCount);
AppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];
nameLblsize=[self sizeOfText:appRecord.name widthOfTextView:222 withFont:[UIFont fontWithName:@"Roboto-Light" size:15] ];
CGRect cell_name_fr = cell.nameLbl.frame;
cell_name_fr.size.height =nameLblsize.height;
cell.nameLbl.frame = cell_name_fr;
cell_name_fr.origin.y = (cell.frame.size.height-nameLblsize.height)/2;
cell.nameLbl.text = appRecord.name;
//cell.nameLbl.backgroundColor = [UIColor grayColor];
NSLog(@"[locArr addObject:appRecord.location]===%@",appRecord.location);
[locArr addObject:appRecord.location];
[descArr addObject:appRecord.desc];
[contArr addObject:appRecord.contacts];
[nameArr addObject:appRecord.name];
[infoArr addObject:appRecord.additional_info];
[addArr addObject:appRecord.address];
[imgArr addObject:appRecord.image];
[bigimgArr addObject:appRecord.big_image];
[catArr addObject:appRecord.categories];
if (!appRecord.appIcon)
{
if (self.mytable_view.dragging == NO && self.mytable_view.decelerating == NO)
{
[self startIconDownload:appRecord forIndexPath:indexPath];
}
}
else
{
imgvwHeight=((10+nameLblsize.height+10+30)/2)-(53/2);
[cell.imgView setFrame:CGRectMake(5, imgvwHeight, 62,53)];
cell.imgView.image = appRecord.appIcon;
}
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
Event *listingNav=[[Event alloc]initWithNibName:@"Event" bundle:nil];
listingNav.loc=[locArr objectAtIndex:indexPath.row];//exception here
listingNav.desc=[descArr objectAtIndex:indexPath.row];
listingNav.contact=[contArr objectAtIndex:indexPath.row];
listingNav.info=[infoArr objectAtIndex:indexPath.row];
listingNav.name=[nameArr objectAtIndex:indexPath.row];
listingNav.address=[addArr objectAtIndex:indexPath.row];
listingNav.image=[imgArr objectAtIndex:indexPath.row];
listingNav.big_image=[bigimgArr objectAtIndex:indexPath.row];
listingNav.type=[catArr objectAtIndex:indexPath.row];
[self.navigationController pushViewController:listingNav animated:NO];
}
异常是: *由于未捕获的异常'NSRangeException'而终止应用程序,原因:'* - [__ NSArrayM objectAtIndex:]:索引5超出边界[0 .. 4]'
答案 0 :(得分:1)
您尝试访问items数组的第6个元素元素,而它只有5个元素。同样在你的代码中有一些tableView委托方法缺失如:- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
所以你也必须收到很多警告。
乍一看,您的代码似乎无法解析您希望它从请求响应中解析的内容。
放置断点以查看响应中的内容,确保将对象添加到数组中并实现UITableView类的所有适当的委托方法。
答案 1 :(得分:0)
请检查Section Method中的行数中的结果数组计数。如果是try,请尝试使用一些静态数组。