从表视图中删除行

Posted

技术标签:

【中文标题】从表视图中删除行【英文标题】:Delete Row from Table view 【发布时间】:2015-06-25 11:13:49 【问题描述】:

我正在开发一个 ios 应用程序。删除tableview中的行并在table view的核心数据中加载数据。当单击删除按钮时应用程序崩溃。 理由就是理由:Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (18) must be equal to the number of rows contained in that section before the update (18), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out)...

_fetchedobj 是一个 NSArray,用于从核心数据中获取数据

//Code is
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

    return _fetchedobj.count;


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    static NSString *MyIdentifier = @"MyReuseIdentifier";
    UITableViewCell *cell = [myTable dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:MyIdentifier];
    
    data = [_fetchedobj objectAtIndex:indexPath.row];
    return cell;


-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
        [tableView beginUpdates];
        NSManagedObject *managedObject = [_fetchedobj objectAtIndex:indexPath.row];
        [self.managedObjectContext deleteObject:managedObject];
        [myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [myTable endUpdates];
    

【问题讨论】:

-tableView:cellForRowAtIndexPath: 的实现是什么样的? 你不应该在删除中做[_fetchedobj removeAtIndex:indexPath.row]吗?否则,您没有更新用于确定行数的数据模型。 【参考方案1】:

你不会忘记从你的数据源数组中移除这个对象吗?

尝试重新填充您的_fetchedobj,因为您必须在删除后配置您的模型。更多详情,请阅读https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html

试试这个

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
        [tableView beginUpdates];
        NSManagedObject *managedObject = [_fetchedobj objectAtIndex:indexPath.row];
        [self.managedObjectContext deleteObject:managedObject];
        // insert this line to your code

        NSMutableArray *newA = [_fetchedobj mutableCopy ];
        [newA removeObjectAtIndex: indexPath.row];
        _fetchedobj = newA;

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

【讨论】:

【参考方案2】:

尝试使用此行删除表格视图行

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

【讨论】:

以上是关于从表视图中删除行的主要内容,如果未能解决你的问题,请参考以下文章

从表视图中删除行

从表视图中删除一行

从表视图中删除行 | NSUserDefaults + NSKeyedArchiver

从表视图中删除行时出现异常。 iOS 目标-C

从表视图中删除行并从 sqlite 数据库中删除记录

从表视图中删除数据会使应用程序崩溃