Show another button next to Delete on UITableViewCell with a swipe - ios

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.

Related

Delete button shows in table view cell before user swipes to delete

I added swipe to delete optionality to a table view cell by implementing
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Perform the real delete action here. Note: you may need to check editing style
// if you do not perform delete only.
NSLog(#"Deleted row.");
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
This is resulting in my table loading in such a way that a delete button is immediately displayed on the left. I only want a swipe to delete option, I don't want any delete button showing when the table first loads. What do I need to change?
Remove the second function (editingStyleForRowAtIndexPath), and change the first one to the one below:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
}
}

Swipe to delete and XLPagerTabStrip

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.

Objective C - Swipe to delete row

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

iOS UITableView - Show delete button upon both swipe left and click cell

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");
}
}

iPhone delete messages like interface

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

Resources