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.
Related
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
I'm new to objective-c and gotten some legacy code for adding new features. My app needs that rows from a table are deleted by swiping then out. The implementation already support the ordering of rows.
I have already read previous questions UITableViewCell, show delete button on swipe and using swipe gesture to delete row on tableview. I don't want to show any button for deleting rows( nor the (-) (red left button) neither the DELETE button that is displayed on the right when clicking the (-) red left button ).
I have already defined the following methods:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"editingStyleForRowAtIndexPath");
return UITableViewCellEditingStyleNone;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"canEditRowAtIndexPath");
return YES; // allow that row to swipe
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"commitEditingStyle");
}
The behavior is that with this "configuration" the delete left red button is displayed. When it is clicked, it displays a DELETE button on the right of the row. Clicking on it run the commitEditingStyle delegate.
Removing the tableView:editingStyleForRowAtIndexPath make disappear the left red button and swipe does not work. Nor I manage to get execution in the commitEditingStyle delegate.
Any suggestion or ideas why the commitEditingStyle delegate is not invoked?
Just write code for deleting row in to
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
I have checked and this works fine for me.
This will delete row on swipe without showing delete button. I hope this will solve your problem.
replace
return UITableViewCellEditingStyleNone
with
return UITableViewCellEditingStyleDelete
I have a UITableView and also implemented the logic to swipe to show the delete button, as such:
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
-(void)tableView:(UITableView *)tableViefw commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete {
//logic to delete...
}
}
However, I need a way for the delete button to also show up when the user clicks on a table cell. Is that possible?
I've seen other answers that suggest using [tableView setEditing: true], but that also shows the red icon on the left and I don't want that.
Thanks.
If you subclass UITabeViewCell, then you can use the didSelectRowAtIndexPath: method to get the clicked cell, you can then show a delete button (whatever design you want), and when that is clicked the cell gets deleted front the datasource and the tableView gets updated, whether there is a framework way of doing it, I do not know.
But good luck anyway :)
Try this method..
When deleting a row you must remove that data form your array. After that you need to reload tableview again to show the updated tableview
- (void)viewDidLoad
{
Your_tableview.editing=YES;
[super viewDidLoad];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(editingStyle == UITableViewCellEditingStyleDelete)
{
NSLog(#"Delete Success");
[Your_Array removeObjectAtIndex:indexPath.row];
[self.Your_tableView reloadData]
}
else
{
NSLog(#"Delete Canceled");
}
}
My app is build in a way that a user enters data inside a UITableView in UIViewcontroller A (one line of data or more) and can save it to disk. In the next UIViewController B he can see all the saved data.
I'm using this:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
}
}
To show a delete button on a cell with a swipe. How can I show an edit button on a single cell (that will appear the same way the delete button appears) next to delete button?
(the edit button will take the data back to the first UIViewController A for editing but that's not really relevant)
I thought about using a swipe gesture but got an error saying I can't use that on a prototype cell. I tried using it on the all UITableVIew but that conflicts with the swipe delete gesture.
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];
}