Ios 当我试图从表中删除行时,应用程序崩溃

Ios 当我试图从表中删除行时,应用程序崩溃,ios,objective-c,iphone,uitableview,Ios,Objective C,Iphone,Uitableview,有时,我可以删除表的第一条记录,但之后我无法删除另一行 我声明一个数组 @implementation TableViewController{ NSMutableArray *tableData; } 查看加载方法 - (void)viewDidLoad { [super viewDidLoad]; tableData = [NSMutableArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"F

有时,我可以删除表的第一条记录,但之后我无法删除另一行

我声明一个数组

@implementation TableViewController{
NSMutableArray *tableData;
}
查看加载方法

- (void)viewDidLoad {
     [super viewDidLoad];

     tableData = [NSMutableArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee", @"Vegetable Curry", @"Instant Noodle with Egg", @"Noodle with BBQ Pork", @"Japanese Noodle with Pork", @"Green Tea", @"Thai Shrimp Cake", @"Angry Birds Cake", @"Ham and Cheese Panini", nil];
}
设置行的标签

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [tableData count];
}

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

    static NSString *simpleTableIdentifier = @"SimpleTableCell";

    SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:simpleTableIdentifier];

    if(cell == nil){
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];    
    return cell;
}
当我滑动时,出现了删除按钮

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    //remove the row from data model
    [tableData removeObjectAtIndex:indexPath.row];
    [tableView reloadData];
}

@end
请尝试以下方法:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{
    //remove the row from data model
    [tableData removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

希望有帮助。

错误信息是什么?没有错误。当我试图删除记录时,应用程序崩溃,日志仅显示11db nothing(无其他内容)。这始终是一条错误消息。在这里,你将得到完整的代码。对我来说,它工作得很好。。此示例中有相同的代码also@Vidhyanand900,并非完全如此,OP和教程之间的区别在于他如何重新加载自定义单元格。在本教程中,它是:cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];,但是OP从nib加载它。如果不解决原始错误,将导致不同的错误。如果我不先尝试,我就不会发布答案。如果你想看看你的答案是否能解决问题,你必须先复制问题。这个答案解决不了问题,这意味着你没有复制这个问题,因此这个答案实际上并没有回答这个问题。这也有助于告诉OP它崩溃的原因,而不仅仅是给他代码,以便他可以复制粘贴它。请参阅此部分了解此答案中显示的代码失败的原因。