我正在解析一个 json url 并加载 UITableview,我正在单击一行并导航到其他 View 。当 我返回到主视图并再次单击一行,我有异常,请帮助。所有用于填充 tableview 的数组都在 viewWillAppear 中分配,并添加了数组元素
-(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]”
最佳答案
您正在尝试访问 items 数组的第 6 个元素,但它只有 5 个元素。另外,在您的代码中,还缺少 tableView 委托(delegate)方法,例如: - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
因此您也必须收到很多警告。
乍一看,您的代码似乎无法解析您期望它从请求响应中解析的内容。
放置断点以查看响应中得到的内容,确保将对象添加到数组中并实现 UITableView 类的所有适当的委托(delegate)方法。
关于ios - 单击 TableView 行时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20368056/