I have a tableview that loads with isEditing = YES. This is a bit of a hack (but a reasonable one I think) because I want one of my cells to always display the 'add' icon. I don't want the other cells to include the 'delete' icon so I am returning UITableViewCellEditingStyleNone for these.
However, I would still like to provide swipe to delete functionality on those cells. Since the tableview is already in edit mode, is this going to be possible?
If not, I will probably achieve the 'add' icon using the cells imageView, but this seems hacky too.
Edit: Similar question found here:
swipe to delete when already in edit mode
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[myArray removeObjectFromindex];
}
[mytableview reloadData];
}
Related
I am using XLPagerTabStrip to swipe between pages. One of those pages has a tableview. I am trying to implement swipe to delete on this tableview, but the DELET button only shows from time to time when swiping.
This is the code I have:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
if ([storiesArray count] >= 1) {
// code to delete row
}
}
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"I am allowing a swipe");
// Return YES if you want the specified item to be editable.
return YES;
}
I can see the NSLog I am allowing to swipe so I know the swipe has been detected, but I can only see the DELETE button occasionally. I can't find the reason why it does not show the delete button. I have searched every post on this, and have asked xmartlabs if implementing their code would affect swipe to delete, but it doesn't make sense that it does work occasionally.
Would anyone have any idea what else I can do to understand why the delete button doesn't show ALWAYS?
Thanks.
You can disable scrolling on XLPagerTabStrip's containerView, so the scrolls will be handled by inner views.
There is also a similar question with an answer.
I am trying to reproduce behaviour of editing bookmarks in iOS Safari like shown here:
I managed to reproduce the look but I am struggling to detect cell click in editing mode when showing both reordering control and disclosure editing accessory type setting:
cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
Basically only the reordering and delete controls seem active, but the delegate method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
does not get called ever, how is this achieved then?
Please note I am using UITableViewController, I have implemented also:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// avoid moving "add new" row
if (indexPath.row > 0) {
return YES;
}
return NO;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath.row > 0 ? UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleInsert;
}
You need to set the allowsSelectionDuringEditing to YES.
From Documentation:
A Boolean value that determines whether users can select cells while
the table view is in editing mode.
If the value of this property is YES, users can select rows during editing. The default value is NO. If you want to restrict selection of cells regardless of mode, use allowsSelection.
iOS8 now provides a way to create swipeable table view cell with actions. In Apple's own Mail app, you can also commit to a default action if you drag the cell all the way (Mail app shows More | Flag | Trash and defaults to Trash if you drag all the way and release). Is there a way to do this in iOS 8?
If your are referring to the swipe to delete on TableViews you can use this code:
//Enable multiple selections during editing
self.tableView.allowsMultipleSelectionDuringEditing = NO;
//Enable editing row
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}
//Do something on delete
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
}
}
What you're looking for is not in a public API yet. Luckily, someone has already done the legwork :)
JASwipeCell
If you notice in the iPad contacts app when you tap on the "+" edit icon to add a new address, the cell does not expand to the left when the icon disappears (see 1st screen capture below). I am trying to do something similar but am having no luck (see 2nd screen capture below). I tried using the solution suggested here but didn't work.
For the record I am not trying to replicate the actual contacts app or integrate with contacts. We're working on something unrelated and my designer just wants to follow this pattern so doing some POC work to see what I can do.
EDIT - Another question, to verify my thinking, is that in the contacts app here what's happening is when the "+" icon is tapped, this cell is set to not editing any more, these text fields are added to the cell, the label changed, and then reloadData is called, right?
EDIT - Thanks for the suggestion Tim, I'm almost there! I had actually worked out 1/2 of it but then was running into the issue that the "+" icon was not animating out, just abruptly disappearing. So I added the reloadRows... call to commitEditingStyle but now the entire cell disappears. Here is my code:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCellEditingStyle editingStyle = UITableViewCellEditingStyleNone;
if (self.showEditingIcon) {
editingStyle = UITableViewCellEditingStyleInsert;
self.showEditingIcon = NO;
}
return editingStyle;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// tableView.editing = NO;
// tableView.editing = YES;
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
You can see from my commented-out lines what I had originally, which was working but as I said, was not animating - the "+" button was just abruptly disappearing. If I tried to call [tableView setEditing:NO animated:YES] it would not work. When I tried to put these calls within a UIView animation block it did not look good - could see cell temporarily shift to left and back. However now with the reload call the cell disappears completely. At the moment I'm not making any changes to my cell (for example I'm not adding text fields, etc...) because I just want to make the "+" button animating out without making the cell expand the left work on the first iteration.
Any help greatly appreciated. Thanks.
The trick is to always be in editing mode such that the cell indention remains constant. When the cell is loaded, you decide what state it is in and return the appropriate editing style, either the "insert" style or "none":
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL shouldShowInsertButton = ...;//check internal state for cell
return shouldShowInsertButton ? UITableViewCellEditingStyleInsert : UITableViewCellEditingStyleNone;
}
Then when the edit button is tapped, you update your internal state and reload the cell:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
...;//update internal state for cell
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
EDIT
Here's a working sample project.
I want to delete rows in a UITableView, I need to provide the delete button not by swipe but in a way similar to delete messages functionality in iPhones. In which on clicking edit a small red circle appears on the left in the cell which rotates when clicked and the delete button is shown. How to implement this?
Like in the image below:
See the Apple documentation here for details on how to achieve this. In particular:
When sent a setEditing:animated: message (with a first parameter of
YES), the table view enters into editing mode where it shows the
editing or reordering controls of each visible row, depending on the
editingStyle of each associated UITableViewCell. Clicking on the
insertion or deletion control causes the data source to receive a
tableView:commitEditingStyle:forRowAtIndexPath: message. You commit
a deletion or insertion by calling
deleteRowsAtIndexPaths:withRowAnimation: or
insertRowsAtIndexPaths:withRowAnimation:, as appropriate.
write this self.navigationItem.leftBarButtonItem = self.editButtonItem; in viewDidLoad method. After that implement this method - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[yourArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
}