Swipe-to-delete doesn't work on iPad - uitableview

I have UITableView with custom cell, Swipe-to-delete works just fine in iPad simulator but it never worked on iPad device.
Here's my code:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView beginUpdates];
if (editingStyle == UITableViewCellEditingStyleDelete)
{
if(_itemsArray == nil)
NSLog(#"\n\nNIL ARRAY\n\n");
NSLog(#"\nindexPath.row = %d\nItemsArray Count:%d",indexPath.row,_itemsArray.count);
int row = [[[_itemsArray objectAtIndex:indexPath.row]valueForKey:#"itemRow"] integerValue];
[_itemsArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[[self delegate]didDeletedBillItemRow:row];
}
[tableView endUpdates];
}
I also implemented layoutSubviews in my custom cell controller:
-(void)layoutSubviews
{
[super layoutSubviews];
}
What do you think the problem is?

May or may not work for you. I spend a while figuring out why it was not working for me. I realized it was because the cell user interaction enabled was off. I don't know why it was still working on the simulator but that fixed it for me.

I only changed the property of selection mode during editing to "Single Selection During Editing" instead of "Multiple Selection During Editing" and I'm now able to delete the rows using swipe-to-delete perfectly.

Related

ios 9 UITableView freezes when deleting cell and does not show confirmation title

I have a code written in Objective-C for deleting cells from tableview.
Here's the code:
- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == (self.numberOfAddsShowing - 1) && (*moreAvailable)) {
return NO;
}
if (isEditable) {
return YES;
}
return NO;
}
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return #"حذف";
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
// If row is deleted, remove it from the list.
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSInteger rowNumber = indexPath.row;
if (rowNumber >= self.addsToShow.count) {
return;
}
HJAdd *adToDelete = [self.addsToShow objectAtIndex:rowNumber];
if (!self.adsToDelete) {
self.adsToDelete = [NSMutableArray array];
}
[self.adsToDelete
addObject:[[NSNumber numberWithInteger:adToDelete.addId] stringValue]];
[self.addsToShow removeObjectAtIndex:rowNumber];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:#[ indexPath ]
withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
[self setEditButtonState];
}
The problem is that when I enter edit mode and try deleting the cell, the confirmation title is not shown and I am unable to delete the cell. When I finish editing mode, the UITableView is no longer scrollable. Swipe to delete is also not working. When I swipe, the tableView enters edit mode but I cannot see the delete button.
When I run the app in Simulator with iOS 8.1 or lower, its working fine.
Here's a video I uploaded to Youtube to show the problem.
Any help would be appreciated. Thanks to all!
You don't need [tableView beginUpdates]; and [tableView endUpdates];.
I think these are for code outside of the commitEditingStyle method.
In the TableView Programming Guide these are not used.
if you have a button bar Item
ViewDidload
self.navigationItem.rightBarButtonItem = self.editButtonItem;
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
TableAccountViewCell *accountCell = [tableView cellForRowAtIndexPath:indexPath];
if ([accountCell.myLabel.text isEqualToString:ADD_USER])
{
return NO;
}
// Return YES if you want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[menuItems removeObjectAtIndex:indexPath.row];
[_myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
//
// Start SQL Delele Account.
//
}
}

UITableView showing delete "-" dash on swipe to delete for cells out of view in other sections

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

delete UITableViewCell,depend on what section it is in

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.

Unable to shift the table view cell when it is swiped to delete

I have a table view with all required datasource and delegate methods.
On running when i swipe the cell,the delete button appears but APPEARS OVER THE cell contents.
The cell doesn't shift.
I expected the functionality to move the cells is present in the following code,but it doesn't
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row)
return YES;
else
return NO;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[[NoteManager sharedManager] removeNoteAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
Please help me to resolve this simple issue
A quick help is appriciated..
When you add subviews to the cell, make sure you add them to the cell's contentView, not the cell.
[cell.contentView addSubview:someSubview];
The frame of the contentView will be adjusted as needed for various cell decorations including the Delete button.

Swipe delete row issue for UITableview

This is gonna drive me crazy! Lots of similar questions, but I still cant seem to figure out why this swipe-to-delete code of mine doesn't work! The 'Delete' button just doesn't appear. Is there some special way of swiping in the UITableview cell, in the simulator, compared to the device?
This is the code,
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
-(void) setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[attachmentsArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[tableView reloadData];
}
}
update - placed break points at canEditRowAtIndexPath,editingStyleForRowAtIndexPath, commitEditingStyle but the control never goes to commitEditingStyle. It just keeps looping between the first two.

Resources