iOS检查IndexPath是不是超出界限

Posted

技术标签:

【中文标题】iOS检查IndexPath是不是超出界限【英文标题】:iOS Check If IndexPath is beyond boundsiOS检查IndexPath是否超出界限 【发布时间】:2014-01-02 09:23:32 【问题描述】:

我想知道是否有人可以提供任何启示,我有一个选定的球员名单。基本上,如果用户点击尚不存在的 indexRow,我想在点击时创建它。

我目前正在做这个检查;

if ([self.tableView cellForRowAtIndexPath:indexPath] == nil) 

这简直是崩溃了;

*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (1)'
*** First throw call stack:

具体方法如下;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    if (indexPath.section == 2 && tableView == self.tableView) 

        if (!self.usersSelectionArray || !self.usersSelectionArray.count || indexPath.row < [self.usersSelectionArray count]) 

            // Create blank array and push to players

            // Push to Players Screen
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle: nil];
            ListOfPlayersViewController *efvc = [storyboard instantiateViewControllerWithIdentifier:@"ListPlayers"];
            [self.navigationController presentViewController:efvc animated:YES completion:nil];

         else 

        if ([self.tableView cellForRowAtIndexPath:indexPath] == nil) 

            // Push to Players Screen
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle: nil];
            ListOfPlayersViewController *efvc = [storyboard instantiateViewControllerWithIdentifier:@"ListPlayers"];
            [self.navigationController presentViewController:efvc animated:YES completion:nil];

         else 

            NSDictionary *cellDict = [self.usersSelectionArray objectAtIndex:indexPath.row];

            // Save Names
            [prefs setObject:[cellDict objectForKey:@"first_name"] forKey:@"teamFirstName"];
            [prefs setObject:[cellDict objectForKey:@"last_name"] forKey:@"teamLastName"];
            [prefs setObject:[cellDict objectForKey:@"avatar"] forKey:@"teamAvatar"];

            // Push to Info Screen
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle: nil];
            PlayerInfoViewController *info = [storyboard instantiateViewControllerWithIdentifier:@"PlayerInfo"];
            [self.navigationController presentViewController:info animated:YES completion:nil];
        
        
    

    if (indexPath.section == 2 && tableView == self.awayTeamTableview) 

        NSDictionary *cellDict = [self.awaySelectionArray objectAtIndex:indexPath.row];

        // Save Names
        [prefs setObject:[cellDict objectForKey:@"first_name"] forKey:@"awayFirstName"];
        [prefs setObject:[cellDict objectForKey:@"last_name"] forKey:@"awayLastName"];

        // Push to Players Screen
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle: nil];
        ManualPlayerViewController *manual = [storyboard instantiateViewControllerWithIdentifier:@"ManualPlayer"];
        [self.navigationController presentViewController:manual animated:YES completion:nil];
    

    int rowNumber = indexPath.row;
    [prefs setInteger:rowNumber forKey:@"rowNumber"];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

【问题讨论】:

您可以添加更多代码吗?你的选择代码和 cellforrowatindex 代码 你能更具体地说明你想要达到的目标吗? 是的,如果您可以显示更多数据源/委托方法的代码,我认为我们可以为您提供帮助 在编辑中添加方法 - 谢谢 如果用户点击不存在的 indexRow,则不会调用 didSelectRowAtIndexPath。 【参考方案1】:

问题可能在tableview数据源方法内部

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

确保您从数组中挑选的元素具有正确的索引

【讨论】:

我不确定会不会?因为这个方法已经被调用了。它正在按下 didSelect 它崩溃【参考方案2】:

在 didSelect 方法中

你可以使用

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath


if(array.count>indexPath.row)

// do you stuff


【讨论】:

这仍然会导致此 LOC 崩溃。 array.count

以上是关于iOS检查IndexPath是不是超出界限的主要内容,如果未能解决你的问题,请参考以下文章

sql索引数组超出界限怎么解决

绘制超出 UIView 的界限

检查 indexPath 处的单元格是不是在屏幕 UICollectionView 上可见

Swift 超出范围.... 使用 tableView[indexPath]

索引超出了数组界限(Microsoft.SqlServer.Smo)

索引 0 超出空数组的界限,有时有效,有时无效