NSInternalInconsistencyException (invalid number of rows)

前端 未结 3 636
陌清茗
陌清茗 2020-12-09 17:54

Whenever I have data in my UITableView and I start deleting, it works fine. However, when I get to the last object in the table, and delete it, it crashes.

相关标签:
3条回答
  • 2020-12-09 18:21

    If you delete the last row in your table, the UITableView code expects there to be 0 rows remaining. It calls your UITableViewDataSource methods to determine how many are left. Since you have a "No data" cell, it returns 1, not 0. So when you delete the last row in your table, try calling -insertRowsAtIndexPaths:withRowAnimation: to insert your "No data" row. Also, you should not call -reloadData anywhere in this method. -endUpdates will take care of reloading the affected rows. Try this out:

    -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            // Delete the row from the data source
            if ([myData count] >= 1) {
                [tableView beginUpdates];
                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                [myData removeObjectAtIndex:[indexPath row]];
    
                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                NSString *documentsDirectory = [paths objectAtIndex:0];
                NSString *somepath = [documentsDirectory stringByAppendingPathComponent:@"something.plist"];
                [myData writeToFile:somepath atomically:YES];
    
                if ([myData count] == 0) {
                    [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                }
                [tableView endUpdates];
            }
        }   
    }
    
    0 讨论(0)
  • 2020-12-09 18:21

    The method tableView:numberOfRowsInSection must always return exact number of rows:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        // Return the number of rows in the section.
        return [myData count];
    }
    

    After having deleted last row, you may want to delete the whole section. Simply call deleteSections:withRowAnimation: within a beginUpdates and endUpdated block;

    -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            // Delete the row from the data source
            [tableView beginUpdates];
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [myData removeObjectAtIndex:[indexPath row]];
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString *somepath = [documentsDirectory stringByAppendingPathComponent:@"something.plist"];
            [myData writeToFile:somepath atomically:YES];
            if ([myData count] == 0) {
                // NEW! DELETE SECTION IF NO MORE ROWS!
                [tableView deleteSections:[NSIndexSet indexSetWithIndex:[indexPath section]] withRowAnimation:UITableViewRowAnimationFade];
            }
            [tableView endUpdates];
        }   
    }
    
    0 讨论(0)
  • 2020-12-09 18:30

    first remove from myData and then delete from tableview.

    -(void)tableView:(UITableView *)tableView commitEditingStyle: 
    (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
            //somehting...
            [myData removeObjectAtIndex:[indexPath row]];
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            //somehting...
        }
    }   
    

    }

    0 讨论(0)
提交回复
热议问题