ios - 单击 TableView 行时出现异常

标签 ios

我正在解析一个 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/

相关文章:

ios - 发送通知时 Azure 通知中心注册消失

ios - 需要访问 iPhone 使用的默认相机快门声音

iphone - 建模一对多关系 Core Data

ios - 您应该如何将推送 token 数据转换为字符串以供查看/调试/记录/放入 http 以发送到服务器?

ios - 如何将 UILabel 文本与框的垂直中心对齐

iphone - 如何检查 iPhone 中是否按下了主页按钮并采取相应措施?

ios - 如何处理 Swift 中的竞争条件读/写问题?

ios - SceneKit 旋转节点两次只执行最后一次旋转?

ios - 选择单元格时,Swift tableView 单元格 subview 闪烁(消失然后重新出现)

ios - Facebook Graph Api 调用不适用于 iOS