I'm trying to delete a UITableviewCell with a swipe gesture.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
With
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_commentsData removeObjectAtIndex:indexPath.row];
_NBComment--;
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
}
The result is this:
The delete buttons remains displayed after the row deletion.
Do you have an idea to make the row deletion smoother?
Thanks in advance
Don't ask me why, but this code works:
[_commentsData removeObjectAtIndex:indexPath.row];
_NBComment--;
[tableView beginUpdates];
[tableView setEditing:NO animated:NO];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
You can try adding this code after deletion:
[tableview reloadData];
Related
I'm trying to create the capability of deleting rows from a tableview and this is how I did it. The error is coming from the
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath] withRowAnimation:UITableViewRowAnimationFade];
Any help would be very much appreciated. Thank you!
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.homeworkArray removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[self.tableView reloadData];
}
the arrayWithObjects: method requires a nil to terminate the list, so you need to say -
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationFade];
}
or even easier, use arrayWithObject:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
Or as #rmaddy points out, use array literal syntax -
[self.tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
Also, you don't need the reloadData call - deleteRowsAtIndexPaths will take care of updating the relevant rows in the table view.
It might be Help full for you I know it Same As Another Answer But it is my Own answer :-
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[arrStations removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
Basically I have an app where you can swipe a tablecell off the screen and it "deletes" the cell. My delete method is called by this: (this happens when the cell reaches a certain point on the phone)
[tableCellTrash addObject: [NSIndexPath indexPathForRow:[numbers indexOfObject:[aPanGestureRecognizer view]] inSection:0]];
UITableViewCell *cell = [numbers objectAtIndex:[numbers indexOfObject:[aPanGestureRecognizer view]]];
[self deleteCell: cell];
This is the delete cell method
-(void)deleteCell: (UITableViewCell *) cell{
[self.tableView beginUpdates];
[numbers removeObject:cell];
[self.tableView deleteRowsAtIndexPaths:tableCellTrash withRowAnimation:UITableViewRowAnimationFade];
[tableCellTrash removeObjectAtIndex:0];
[self.tableView endUpdates];
//[self.tableView reloadData];
}
I have no idea what's wrong, the cell doesn't delete. Any help would be appreciated.
tableviews have a delegate method to handle the event for delete:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[tableView reloadData];
}
I've been reading a lot about updating a UITableView after you remove rows by both IndexPath and the datasource, which it seems like comes down to [tableView startUpdates], run whatever update code you have, then [tableView endUpdates].
Despite following numerous examples to a T, and utilizing [tableView deleteRowsAtIndexPaths:<indexPaths> withRowAnimation:UITableViewRowAnimationTop];
, I can't seem to make my rows delete in any way but what the following depicts:
http://www.youtube.com/watch?v=l6ov1FR0R4g&feature=youtu.be (essentially, if there are n > 1 rows, the row will simply blink out, and if there is n = 1 row, it will slide outward, stop, then disappear).
I'd really like a smoother behavior, like, perhaps, the row slides out, as the row slides up. Here's the code executing the deletion:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
int idx = [indexPath indexAtPosition:1];
LoLoanedItemDoc *itemDoc = [[self items] objectAtIndex:idx];
// Delete notification
[[UIApplication sharedApplication] cancelLocalNotification:[[itemDoc data] notification]];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationTop];
// Remove from datasource
[[self items] removeObject:itemDoc];
[tableView endUpdates];
// Delete folder
[itemDoc deleteDoc];
}
I've done it two ways, both of which work for me.
Delete the item before you perform the updates.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//Delete the item from the data source
[self removeProductFromQuoteAtIndexPath:indexPath];
//Do the updates
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[shipping removeObjectAtIndex:indexPath.row - 1];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
Are you performing any type of operation on the cell before it deletes? (like closing the drawer animation you have).
I have a problem, I want to delete row in UITableView but don't use swipe button delete. I want call function removeCell after condition. My problem is I cannot delete first row in UITableView because I don't know get indexPath of row to delete.
If I swipe Delete button, it work perfect. But I want don't use Delete Button.
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
deleteindexPath = indexPath;
/*[_tableView beginUpdates];
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[_tableView endUpdates];*/
[self removeCell];
}
removeCell():
-(void) removeCell
{
[_tableView beginUpdates];
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:deleteIndexPath] withRowAnimation:UITableViewRowAnimationFade];
// }
[_tableView endUpdates];
}
I try set deleteIndexPath = [_tableView indexPathForCell:cell]; in cellForRowAtIndexPath() but not work, crash.
Can you have suggestion?
Just try this
-(void) removeCell
{
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:deleteIndexPath] withRowAnimation:UITableViewRowAnimationFade];
//Now remove data from your tableArray also..
[_tableView reloadData];
}
My problem is that my delete method do not delete the right row!
In this tableView the user can save localNotifications for a special date. So if I create more than one localNot, the delete method does not delete the selected row! It deletes the last entry only ...
26.02.2012 delete third
27.02.2012 delete second
etc... delete first
Whats wrong?
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if(tableView.editing && editingStyle== UITableViewCellEditingStyleDelete)
{
[tableView beginUpdates];
[[UIApplication sharedApplication] cancelLocalNotification:notification];
[notificationsArray removeObjectAtIndex:indexPath.row];
NSLog(#"%#", notification);
// Animate the deletion from the table.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[tableView endUpdates];
[tableView reloadData];
}
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
// UITableViewCellEditingStyleNone;
return YES;
}