jeudi 25 juin 2015

Delete Row from Table view

I am developing an IOS app.delete the row in table view and data load in core data in table view.When Click the delete button App crash. Reason is reason: '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 is an NSArray to Fetch data from core data

//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];
        }
    }

See more ..

Aucun commentaire:

Enregistrer un commentaire