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;
}
Related
I am encountering what seems like a bug with swipe to delete for a UITableView, when I swipe to delete in a visible section it shows the red delete dash in some rows for sections out of view. This happens in IOS SDK 8.2.
Sorry, tried to post an image, but I don't seem to have enough reputation to post an image.
Please provide any thoughts on what could be causing this, Thanks.
View Image http://i.imgur.com/xSdvdJH.png
Edited
Code used is Standard UITableView Delegates and Datasource Delegates to enable Swipe to Delete.
- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if ((self.sectionForCellSwiped == indexPath.section) && self.rowForCellSwiped == indexPath.row) {
return UITableViewCellEditingStyleDelete;
}else{
return UITableViewCellEditingStyleNone;
}
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.editEnabled) {
return YES;
}else{
return NO;
}
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
if ([self.DataSource numberOfForCategoryAtIndex:indexPath.section] == 1) {
[self.DataSource removeItem:removedItem];
[self.tableView beginUpdates];
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]
withRowAnimation:UITableViewRowAnimationFade];
[self.tableView reloadData];
[self.tableView endUpdates];
}else{
[self.DataSource removeItem:removedItem];
[self.tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView reloadData];
}
}
[self.tableView reloadData];
[self.tableView setNeedsLayout];
[self.tableView layoutIfNeeded];
}
I got a problem: I have a 2 sections UITableView,i want the cells in section one can be delete(when swipe left show the delete),and the cells in section two cannot be delete.
somebody give me a hint ,thanks.
You can implement the tableView:canEditRowAtIndexPath: method from the UITableViewDataSource protocol to determine whether the row can be deleted or not...
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 1) {
return NO;
} else {
return YES;
}
}
Then, to handle the delete you will use the tableView:commitEditingStyle:forRowAtIndexPath: method...
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// Check editing style and take action (delete or edit)
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(#"Delete row %ld in section %ld", (long)indexPath.row, (long)indexPath.section);
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}
}
I'm assuming you have already implemented the UITableViewDataSource delegate protocol, so it should only be a matter of adding these methods.
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];
I am populating my iOS table view with the names of files inside my database. Everything is displayed in the tableView as expected. The problem comes when i delete it, it deleted the entries in the database but the entry doesn't fade away in the UITableView. Here is my code:
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
DBHandler *handler;
[handler ResumeID:[mydataList objectAtIndex:indexPath.row]]; //mydataList contains the array that contains name of all the entries in the DB and is displaying them on the tableview.
[mydataList removeObjectAtIndex:indexPath.row];
NSLog(#"%ld",indexPath.row);
// Animate the deletion from the table.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
You are actually passing the value which is deleted before, to the DBHandler. Check the code,
if (editingStyle == UITableViewCellEditingStyleDelete)
{
DBHandler *handler ;
[handler ResumeID:[mydataList objectAtIndex:indexPath.row]]; // Pass the object from the dataSource before deleting the object from the dataSource.
[mydataList removeObjectAtIndex:indexPath.row]; // Remove object after passing it to the DBHandler
NSLog(#"%ld",indexPath.row);
// Animate the deletion from the table.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
Try this.
Its a bug of ios7.. the tablerow animations are broken! My fix was to fadeOut the cell right before the tableViewRowAnimation..
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// hide cell, because animations are broken on ios7
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
[tableView cellForRowAtIndexPath:indexPath].alpha = 0.0;
}
[tableView deleteRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationMiddle];
}
after deleting refresh the view
[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).