使用UITableView时应用程序崩溃

时间:2017-02-17 09:48:12

标签: ios objective-c uitableview

第一个屏幕上的

用户输入数据&使用prepareforsegue方法在第二个ViewController中显示它。在第二个屏幕用户选择多行进行删除然后再次用户选择剩余的行以删除应用程序崩溃。这是我的代码

  

*由于未捕获的异常'NSRangeException'而终止应用程序,原因:'* - [NSMutableArray removeObjectsAtIndexes:]:索引集中的索引5超出边界[0 .. 4]'

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return mAryValue.count;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * strIdent=@"id1Cell";

    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:strIdent];

    if (cell==nil)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strIdent];
    }

    cell.textLabel.text=[mAryValue objectAtIndex:indexPath.row];

    return cell;

}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.editing)
    {
        return;
    }

    [_tblView deselectRowAtIndexPath:[_tblView indexPathForSelectedRow] animated:NO];
    UITableViewCell *cell = [_tblView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryNone)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [mArySel addObject:indexPath];
    }
    else if(cell.accessoryType == UITableViewCellAccessoryCheckmark)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [mArySel removeObjectIdenticalTo:indexPath];
    }  
}

-(void)getData:(NSMutableArray *)userValues
{
    mAryValue=userValues;
}

- (IBAction)btnDelete:(id)sender
{
        if (mAryValue==nil || mAryValue.count==0)
        {

            UIAlertController * alert=   [UIAlertController
                                          alertControllerWithTitle:@"Alert"
                                          message:@"Please Enter Value"
                                          preferredStyle:UIAlertControllerStyleAlert];

            UIAlertAction* ok = [UIAlertAction
                                 actionWithTitle:@"OK"
                                 style:UIAlertActionStyleDefault
                                 handler:^(UIAlertAction * action)
                                 {
                                     [alert dismissViewControllerAnimated:YES completion:nil];

                                 }];
            UIAlertAction* cancel = [UIAlertAction
                                     actionWithTitle:@"Cancel"
                                     style:UIAlertActionStyleDefault
                                     handler:^(UIAlertAction * action)
                                     {
                                         [alert dismissViewControllerAnimated:YES completion:nil];

                                     }];

            [alert addAction:ok];
            [alert addAction:cancel];

            [self presentViewController:alert animated:YES completion:nil];
        }
    else
    {
        NSMutableIndexSet *indicesToDelete = [[NSMutableIndexSet alloc] init];
        for (NSIndexPath *indexPath in mArySel)
        {
            [indicesToDelete addIndex:indexPath.row];
        }

        if (!(indicesToDelete==nil) || !(indicesToDelete.count==0))
        {
            [mAryValue removeObjectsAtIndexes:indicesToDelete];

        }
        [_tblView reloadData];
    }


}

请建议我。 谢谢。

1 个答案:

答案 0 :(得分:1)

  

[NSMutableArray removeObjectsAtIndexes:]:索引集中的索引5超出   界限[0 .. 4]'

您正尝试删除document.getElementsByClassName("lyrics")[0].onclick = function () { var x = document.getElementsByClassName("showLyrics")[0]; var y = document.getElementsByClassName("lyrics")[0]; if (x.style.display === "none" || x.style.display === "") { x.style.display = "table-row"; y.innerHTML = '<a href="#" class="lyrics">Hide Lyrics</a>'; } else { x.style.display = "none"; y.innerHTML = '<a href="#" class="lyrics">Show Lyrics</a>' } 上不存在的索引处的对象。如错误所示,您要删除array,但您的数组只有4个项目。

objectAtIndex:5