简体   繁体   English

尝试将第0行插入第0部分,但更新后第0部分只有0行

[英]attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update

I have this crash. 我有这个崩溃。

I have tried everything. 我已经尝试了一切。 I have searched in Google for 3 days to understand why my app crashes, and I didn't find anything. 我在Google中搜索了3天,以了解我的应用崩溃的原因,但没有找到任何东西。 I found a lot of similar posts, tried to used some from here and there, but still getting a crash. 我发现了很多类似的帖子,试图从那里到那里使用一些帖子,但是仍然崩溃。

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update' 由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“试图将第0行插入第0节,但更新后第0节只有0行”
First throw call stack: 首先抛出调用堆栈:

My .m file: 我的.m文件:

#import "PlayersViewController.h"
#import "Player.h"
#import "PlayerCell.h"

@interface PlayersViewController ()
@end

@implementation PlayersViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlayerCell *cell = (PlayerCell *)[tableView
                                      dequeueReusableCellWithIdentifier:@"PlayerCell"];
    Player *player = [self.players objectAtIndex:indexPath.row];
    cell.nameLabel.text = player.name;
    return cell;
}

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

- (void)playerDetailsViewControllerDidCancel:
    (PlayerDetailsViewController *)controller
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)playerDetailsViewControllerDidSave:
    (PlayerDetailsViewController *)controller
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"AddPlayer"])
    {
        UINavigationController *navigationController =
        segue.destinationViewController;
        PlayerDetailsViewController
        *playerDetailsViewController =
        [[navigationController viewControllers]
         objectAtIndex:0];
        playerDetailsViewController.delegate = self;
    }
}

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

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

- (void)playerDetailsViewController:
(PlayerDetailsViewController *)controller
                       didAddPlayer:(Player *)player
{
    [self.players addObject:player];

    [NSThread sleepForTimeInterval:0.5];

    [self.tableView beginUpdates];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];

    [self dismissViewControllerAnimated:YES completion:nil];
}

@end

I hope you can help. 希望您能提供帮助。

Have you give the cell identifier to PlayerCell from storyboard? 您是否已从情节提要中将单元标识符提供给PlayerCell? Define viewDidLoad method in your .m file. 在.m文件中定义viewDidLoad方法。

I your .h file add delegate and datasource for table view, Like UITableViewDataSource and UITableViewDlegate. 我的.h文件为表视图添加了委托和数据源,例如UITableViewDataSource和UITableViewDlegate。

After that in your .m file and in viewDidLoad method add, 之后,在您的.m文件和viewDidLoad方法中添加,

self.tableView.delegate = self;
[self.tableView setDataSource : self];

This might help you. 这可能对您有帮助。

You can log the array of players and see it is not nil. 您可以登录玩家数组,然后查看它是否为nil。 For your player methods and logic make new model file and write player logic there and import it in your table view. 为您的播放器方法和逻辑创建新的模型文件,并在其中写入播放器逻辑,然后将其导入到表格视图中。

Between beginUpdates and endUpdates you must modify both the tableview and the datasource in a matching way. 在beginUpdates和endUpdates之间,必须以匹配的方式修改tableview和数据源。 You don't do that. 你不要那样做。 You only modify the tableview. 您仅修改表视图。

And I'd recommend to learn and use the Objective-C array syntax and Objective-C literals. 而且,我建议学习和使用Objective-C数组语法和Objective-C文字。 "[self.players objectAtIndex:indexPath.row]" becomes "self.players [indexPath.row]" and "[NSArray arrayWithObject:indexPath]" becomes "@[indexPath]". “ [self.players objectAtIndex:indexPath.row]”变为“ self.players [indexPath.row]”,“ [NSArray arrayWithObject:indexPath]”变为“ @ [indexPath]”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “尝试将第0行插入第0部分,但更新后第0部分只有0行” - 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update' Swift-“尝试将第0行插入第0节,但更新后第0节只有0行” - Swift - “attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update” “尝试将第0行插入第0部分,但更新后第0部分只有0行”错误 - “Attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update” Error 尝试将第 3 行插入第 0 节,但更新后第 0 节中只有 3 行 - attempt to insert row 3 into section 0, but there are only 3 rows in section 0 after the update 尝试插入行时“尝试将第 0 行插入第 0 节,但更新后第 0 节中只有 0 行” - "attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update" when trying to insert row 在表中插入一行时,“试图将第0行插入第0部分,但更新后第0部分只有0行” - 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update' when inserting a row into a table 'NSInternalInconsistencyException',原因:“试图将第0行插入第0节,但更新后第0节只有0行” - 'NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update' 拖放时出现错误“尝试将第 4 行插入第 1 节,但更新后第 1 节中只有 4 行” - Getting an error "attempt to insert row 4 into section 1, but there are only 4 rows in section 1 after the update" while drop and drag 'NSInternalInconsistencyException',原因:“试图将第4行插入第0节,但更新后第0节只有4行” - 'NSInternalInconsistencyException', reason: 'attempt to insert row 4 into section 0, but there are only 4 rows in section 0 after the update' SwiftUI 应用程序因“尝试将第 2 行插入第 0 部分,但更新后第 0 部分中只有 2 行”错误而崩溃 - SwiftUI app crash with “attempt to insert row 2 into section 0, but there are only 2 rows in section 0 after the update” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM