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.
Related
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.
//
}
}
I wanna delete a particular row while I drag it.
Here is part of code.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableData removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableData removeObjectAtIndex:indexPath.row];
[tableView reloadData];
}
}
Do you set delegate ?
self.tableView.delegate = self;
With corresponding How do I get the delete button to show when swiping on a UITableViewCell?
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// delete from model
// [self.tableView reloadData]
}
}
You can put on breakpoints into your code for check call functions
yagnesh dobariya's answer is a working solution, and I would like to propose a modification that makes it more efficient.
Since you are only deleting one row at a time, you can keep using -deleteRowsAtIndexPaths:withRowAnimation: as in your original code. Just sandwich it between -beginUpdates and -endUpdates.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView beginUpdates];
[tableData removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
}
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 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.
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.