iOS检查IndexPath是否超出范围

时间:2014-01-02 09:23:33

标签: ios objective-c arrays uitableview nsindexpath

我想知道是否有人可以放弃任何光线,我有一个选定的球员名单。基本上,如果用户点击尚不存在的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];
}

2 个答案:

答案 0 :(得分:0)

问题可能出在tableview数据源方法

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

确保你是数组中的pickin元素具有正确的索引

答案 1 :(得分:0)

在didSelect方法中

你可以使用

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

if(array.count>indexPath.row)
{
// do you stuff
}
}