单击表格视图行时出现异常
Posted
技术标签:
【中文标题】单击表格视图行时出现异常【英文标题】:Getting exception when clicking table view row 【发布时间】:2013-12-04 05:57:52 【问题描述】:我正在解析一个 json url 并加载 UITableview,我正在单击一行并导航到其他视图。当 我正在返回主视图并再次单击一行,我有异常,请帮助。用于填充 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:]: index 5 beyond bounds [0 .. 4]”
【问题讨论】:
【参考方案1】:您正在尝试访问 items 数组的第 6 个元素,而它只有 5 个元素。此外,在您的代码中,缺少 tableView 委托方法,例如:- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
,因此您也必须收到很多警告。
乍一看,您的代码似乎无法解析您期望它从请求响应中解析的内容。
设置断点以查看响应中的内容,确保将对象添加到数组中并实现 UITableView 类的所有适当委托方法。
【讨论】:
我有 numberOfRowsInSection 好的。您在那里返回错误的数组计数。什么是条目数组?解析响应后,您将每个元素添加到不同的数组中。所以你所有的数组都有一个项目。用您当前的代码点击第六个单元格会查找不存在的[array objectAtIndex:6]
,因此您会崩溃。
yes entries 是解析后存储所有元素的数组【参考方案2】:
请在部分方法中的行数中检查您的结果数组计数。如果是写入尝试使用一些静态数组。
【讨论】:
以上是关于单击表格视图行时出现异常的主要内容,如果未能解决你的问题,请参考以下文章