簡體   English   中英

從表視圖中刪除行

[英]Delete Row from Table view

我正在開發IOS應用程序。 刪除表視圖中的行,並在表視圖中刪除核心數據中的數據。 單擊刪除按鈕時,應用程序崩潰。 原因是原因: 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];
    }
}

您是否忘了從數據源數組中刪除該對象?

請嘗試重新填充_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];
    }
}

嘗試使用此行刪除表格視圖的行

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM