【问题标题】:No update or numberOfRowsInSection after delete with commitEditingStyle使用 commitEditingStyle 删除后没有更新或 numberOfRowsInSection
【发布时间】:2012-11-26 12:30:45
【问题描述】:

使用编辑/删除或滑动删除手势删除项目后,我的表格没有更新。如果我进行编辑/删除,删除按钮不会消失。它以按下/卡住的状态停留在那里。然后当我单击完成时,删除按钮消失。这篇文章的底部附有一个截图。

我的 numberOfRowsInSection 代码在应用启动时执行,而不是在删除项目后执行。

我正在使用 UITableViewController。我的 .h 文件中的定义:

@interface BNVFavoritesTableViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>{    
  BNVAppDelegate *appDelegate;    
  IBOutlet UITableView *myTable;
}

@property (retain, nonatomic) IBOutlet UITableView *myTable;
@end

这是我的 .m 文件中的相关代码:

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"number of rows:\t%d", [appDelegate.favoritesArray count]);
    return [appDelegate.favoritesArray count];
}

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    //Get the object from the array.
    BNVFavorite *favoriteObj = [appDelegate.favoritesArray objectAtIndex:indexPath.row];

    //Set the name.
    cell.textLabel.text = favoriteObj.name;

    // Set up the cell
    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // If row is deleted, remove it from the list.
    if (editingStyle == UITableViewCellEditingStyleDelete){
        NSLog(@"before delete\t%d", [appDelegate.favoritesArray count]);        
        BNVFavorite *selectedObject = [appDelegate.favoritesArray objectAtIndex:indexPath.row];
        NSString *selectedName =  selectedObject.name;
        // delete from sqlite database
        [appDelegate removeFavoriteFromDB:selectedName];
        // delete from memory array
        [appDelegate.favoritesArray removeObjectAtIndex:indexPath.row];
        // delete in user interface
        [myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        NSLog(@"after delete\t%d", [appDelegate.favoritesArray count]);

    }

}

删除时,我在日志中看到“删除前”和“删除后”消息(如预期的那样),但没有“行数”消息。

表视图中的数据源和委托出口连接到 TableViewController 类。我也尝试在 ViewDidLoad 方法中执行此操作,但这并没有什么不同。在我的commitEditingStyle 末尾强制使用[myTable reloadData]; 也无济于事。

最后,这是那个“卡住”删除按钮的屏幕截图。 http://i.stack.imgur.com/ukUu0.png

点击几次会导致 NSRangeException/out of bounds 错误,表示执行了来自commitEditingStyle的代码。

【问题讨论】:

  • 嘿,在 appDelegate.favoritesArray 中,对象是否被删除?
  • 是的,已删除。如果我点击删除按钮几次,我得到 NSRangeException 错误,正如预期的那样。
  • 您的行是否已从表中删除..?
  • 是的,但那不是问题。
  • 我在回答中提到了你的回答..:)

标签: iphone xcode uitableview


【解决方案1】:

经过几个小时的搜索,我找到了问题所在。结果从情节提要中的 tableview 到我的控制器类中的 myTable 的引用出口不再存在。我一定是不小心把它取下来了。当我注意到将myTable 替换为self.tableView 时代码开始工作时,我发现了这一点。

【讨论】:

    【解决方案2】:

    您需要在beginUpdatesendUpdates 之间添加删除动画。

    [myTable beginUpdates];
    [myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [myTable endUpdates];
    

    【讨论】:

    • 不幸的是,这没有帮助。似乎没有什么不同。不过感谢您的帮助!
    【解决方案3】:

    只需在连接检查器中检查您的连接即可。

    或者如果连接正确,那么 删除记录后重新加载表

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        // If row is deleted, remove it from the list.
        if (editingStyle == UITableViewCellEditingStyleDelete){
            NSLog(@"before delete\t%d", [appDelegate.favoritesArray count]);        
            BNVFavorite *selectedObject = [appDelegate.favoritesArray objectAtIndex:indexPath.row];
            NSString *selectedName =  selectedObject.name;
            // delete from sqlite database
            [appDelegate removeFavoriteFromDB:selectedName];
            // delete from memory array
            [appDelegate.favoritesArray removeObjectAtIndex:indexPath.row];
            // delete in user interface
            [myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            NSLog(@"after delete\t%d", [appDelegate.favoritesArray count]);
            [myTable reloadData];
        }
    
    }
    

    【讨论】:

    • 已经尝试过这个:( 强制 [myTable reloadData]; 在我的 commitEditingStyle 结束时没有帮助。
    • 好的..可能是你的数据没有在数据库中正确删除......只需检查你的数据库
    • 不,也不是这样:数据已正确删除。我昨天找到了解决方案,并且已经发布了答案。问题是我的 Tableview 插座不再正确连接到我的班级。
    猜你喜欢
    • 2023-03-29
    • 1970-01-01
    • 2020-05-07
    • 2020-01-10
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多