2

I am trying to bypass the red button delete (editingstyledelete) by setting up a gesture of swipe and then using IBAction to call a delete of the row that the swipe was performed in. The app crashes and I get a error of : NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]

- (IBAction)deleteSwipe:(id)sender{
    [self deleteRow:self.tableView forCell:sender];
}

-(void) deleteRow:(UITableView *)tableView forCell:(UITableViewCell *)bCell{
    NSIndexPath *indexPath = [tableView indexPathForCell:bCell];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [self.queue removeObjectAtIndex:indexPath.row];
    [tableView reloadData];
}

i setup a breakpoint exception and it points to the: [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; and in the report log it says that the index path value is nil. I have used the 'deleteRowsAtIndexPaths' method before and did not find this problem.

edit: updated code:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.queue removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        [self.tableView reloadData];
        }
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleNone;


}


-(BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{
    return NO;
}

-(BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}


- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
snapfish
  • 175
  • 1
  • 14
  • if i comment out the 'deleteRowsAtIndex' line, the method works but the screen just reloads since I put a reloadData in there and such a sudden transition does not look good. If there is another way to initiate a delete animation onto the cell, I'd be happy to comment out the 'deleteRowsAtIndexPaths' and opt for that route. – snapfish Feb 24 '14 at 06:16
  • Are `tableView` and `self.tableView` the same object? A tableView parameter is passed into the `deleteRow:forCell:` method, but it is ignored when you call `indexPathForCell:`. – bneely Feb 24 '14 at 06:22
  • thanks, i made the edits but the same problem still arises – snapfish Feb 24 '14 at 06:25
  • Did you check in the debugger if `tableView` and `self.tableView` are the same object? – bneely Feb 24 '14 at 06:26
  • sorry, im pretty new at this. How do i check the debugger if they r the same object? Also, it shouldnt matter anymore since I edited to only use the parameter variable – snapfish Feb 24 '14 at 06:53

1 Answers1

5

You can do this by implementing following UITableView deligate methods

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleNone; //if you don't want to show delete button
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
        return YES; // allow that row to swipe
        //return NO; // not allow that row to swipe
}

// Override to support editing/deleteing the table view cell on swipe.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleNone)
    {
        //--- your code for delete -----
    }
}
Haresh Ghatala
  • 1,996
  • 17
  • 25
  • this was my original method but I wanted to be able to swipe and delete WITHOUT having the click the red button i.e. swipe calls the method to delete. – snapfish Feb 24 '14 at 06:52
  • ya then also this code works. You just have to do
    `return UITableViewCellEditingStyleNone;` in `editingStyleForRowAtIndexPath` and
    wirte your delete code in `if (editingStyle == UITableViewCellEditingStyleNone)` of method `commitEditingStyle`
    – Haresh Ghatala Feb 24 '14 at 06:56
  • 1
    and by using this it will also not show delete button in your cell – Haresh Ghatala Feb 24 '14 at 06:57
  • 1
    after making the edits u suggested, a left swipe no longer does anything to the cell. I also tried setting up a swipe gesture and calling the commitEditingStyle in it with the sender as the cell the swipe was performed in. Neither worked. – snapfish Feb 24 '14 at 07:28
  • 1
    i think u misunderstood by the red button. the red button is the red box on the right side that appears with the word delete in it. NOT the red circle on the left side. I can get rid of the red circle but not the red box. – snapfish Feb 24 '14 at 07:37
  • I don't know what happen with you, but i have put here my working code in which i have perform mark on swipe and it working well. can you plese make more focus on it or put your code so i get the problem – Haresh Ghatala Feb 24 '14 at 07:55
  • i updated the question with my code. If i changed the 'commitEditingStyle' to 'if (editingStyle == UITableViewCellEditingStyleNone)' then nothing happens when i try to swipe it in the simulator. – snapfish Feb 24 '14 at 08:35
  • Could you please edit ur answer with the proper code you suggested so I can compare and make sure I'm not doing something wrong? – snapfish Feb 24 '14 at 08:40
  • Do i need to add an IBAction after the changes because when I currently make the changes, nothing happens and apple says UITableViewCellEditingStyleNone = no editing control. It used to create ur custom controls. – snapfish Feb 24 '14 at 17:11
  • No you don't need to implement any IBAction methods it will works with out it. just add you code for deletion in method commitEditingStyle where i commented. – Haresh Ghatala Feb 26 '14 at 05:27
  • @snapfish did you manage to get it working? Im having exactly the same issue. Did you come to any solution? – kitimenpolku Feb 26 '15 at 12:51