-2

I have a tableView and a delete implemented when swiping to the left (normal way). When I hit the delete button, the record is deleted from the db but not form the view. I want to remove it from the table instantly. In order to see that the record was deleted I need to go back to the previous view and come back again to the tableView and only then the record is gone.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
    NewArt *currArt = [self.lN_Dep objectAtIndex:indexPath.row];

    self.deptsub = [self.lN_Dep subarrayWithRange:NSMakeRange(indexPath.row, (self.lN_Dep.count - indexPath.row))];

    self.selectedURL = [currArt.link absoluteString];
    self.selected_ID = (NSString *)currArt.art_ID;
    self.selectedArt = currArt;

    NSArray *ArrayOfArtIDs_old = [ArtString componentsSeparatedByString:@","];
    NSMutableArray *ArrayOfArtIDs_new = [[NSMutableArray alloc]init];
    ArrayOfArIDs_new = [[NSMutableArray alloc] initWithArray:ArrayOfArtIDs_old];

    for (NSString *item in ArrayOfArtIDs_old) {
        if ([item isEqualToString:self.selected_ID]) {
            [ArrayOfArtIDs_new removeObject:self.selected_ID];
            NSString *updateArt = [self updatedArtIDs:ArrayOfArtIDs_new];
            if ([updateArt isEqualToString:@"true"]) {
                [self.tableView beginUpdates];
                [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                [_lN_Dep removeObjectAtIndex:indexPath.row];
                //[_lN_Dep removeObject:indexPath];
                [self.tableView endUpdates];
            }
        }
    }
    [self bookMarkedFeed];
}

The program breaks when I press delete and this line is highlighted.

[_lN_Dep removeObject:indexPath]; 

or

[_lN_Dep removeObjectAtIndex:indexPath.row];
dan
  • 23
  • 7

4 Answers4

1

Sorry, don't have the rep to comment... The latest error is because your NSArray is not mutable. Try:

NSMutableArray *temp = [NSMutableArray arrayWithArray:self.lN_Dep];
[temp removeObjectAtIndex:indexPath.row];
self.lN_Dep = [NSArray arrayWithArray:temp];
AstroCB
  • 12,337
  • 20
  • 57
  • 73
Nilskilz
  • 98
  • 1
  • You cannot write the last line. It is incompatible to convert NSMutableArray to NSArray. – dan Mar 11 '15 at 15:46
  • actually 'copy' is cleaner, but I don't see what the problem with this is? – Nilskilz Mar 11 '15 at 15:51
  • http://stackoverflow.com/questions/1768937/how-do-i-convert-nsmutablearray-to-nsarray – Nilskilz Mar 11 '15 at 15:51
  • 1
    I made a slight change to your code. I don't know why but this works fine. btw my LN_dept array is mutable array. haha this is so confusing. But it works fine now. – dan Mar 11 '15 at 15:56
0

Use this below line to delete with animation,

[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
DilumN
  • 2,889
  • 6
  • 30
  • 44
  • I am getting this error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (4), 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).' – dan Mar 09 '15 at 17:05
  • Try this - [NSArray arrayWithObjects:indexPath, nil] instead [NSArray arrayWithObject:indexPath] – DilumN Mar 09 '15 at 17:14
  • Nope it does not work. Same error. Wait do I need to replace the word NSArray? – dan Mar 09 '15 at 18:34
0

Use the code suggested to delete your row:

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

The error your getting is because the tableView datasource still contains the record for the row you've deleted. You need to delete the object from your array. I assume it will be:

[self.lN_Dep removeObjectAtIndex:indexPath.row];
Nilskilz
  • 98
  • 1
  • I edited the code. Please see above. I am still getting an error message, after trying what you've suggested. – dan Mar 11 '15 at 14:30
0

_lN_Dep is a NSArray (_NSArrayI -> _NSArrayI(mmutable) is a concrete private implementation of NSArray class cluster), therefor it doesn't support any method for removing. Make it a NSMutableArray or copy it to a mutable temp array, remove stuff there, assign it back (as amicable copy).

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178