I'm trying to implement the swipe left to display the delete button.
The cell moves left, but there are no visible buttons. Why does this happen? What is the solution?
To be clear, I have searched on StackOverflow and tried at least a dozen posted "solutions", but none of them have worked for me.
Thanks in advance.
I tried using the following code, which does seem to make the table cell move, but the log does not show NSLog(#"commitEditingStyle");. Why does this happen?
// Swipe to delete.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"commitEditingStyle");
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(#"UITableViewCellEditingStyleDelete");
[listData removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView reloadData];
}
}
And I have tried adding:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
Here is a screenshot:
The problem was that the table size was larger than my screen size. The solution is to set the constraints to match the width of the view.
// During startup (-viewDidLoad or in storyboard) do:
self.tableView.allowsMultipleSelectionDuringEditing = NO;
- (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) {
//add code here for when you hit delete
}
}
Related
I want to implement "slide to delete" table cell functionality. This should show red color when cell is slide & delete row on full slide. This is same as in android list & gmail app slide to archive feature. Has anyone implemented this?
You have to set editing to yes for your tableview and you have to implement canEditRowAtIndexPath and commitEditingStyle delegate of your table view like :
In viewDidload,
[yourTableView setEditing:YES];
and your delegates should be like,
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
// here you can delete object from your datasource(array) also if needed.
}
}
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
}
}
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");
}
}
I am developing an app which has a PageViewController and two view controllers as children of it. One of the Child View controllers has a UITableView.
The problem occurs when I swipe the child view controller. I want to have two functionalities like there should be a swipe for delete on tableView.
When I swipe the child view controller it sends swipe to pageViewController. So it does not show the delete button when we swipe a table view cell.
I want to have two functionalities on swipe event so that there would be both:
Swapping of view
Show red delete button on row
Try this,
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//delete your data here
}
}
You should return YES to this method of the UITableViewDataSource protocol to tell the tableView your cell can respond to the swipe to let appear the delete button:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
and then delete your data in this method, called when the user taps the delete button:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Remove here your data
...
// This line manages to delete the cell in a nice way
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
I have a custom TableViewCell (without Interface Builder) when I use the swipe gesture to trigger the DeleteConfirmationButton to appear and then touch it the button disappears as usual.
But when I set the whole TableView in editing mode with the default edit button:
[self.navigationItem setRightBarButtonItem:[self editButtonItem]];
and then touch the DeleteConfirmationButton it only gets dark red and don't dissapears.
Any ideas?
PS: Is it possible to don't show the button when using the swipe gesture (so it only is available in editing mode)?
EDIT: To get an idea what I mean (I'm only using the delete button to clear the stars)
To remove the delete button you can try:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// delete stars
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
The answer to your PS is yes. You can use something along the lines of:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if (!tableView.editing) {
return UITableViewCellEditingStyleNone;
}
return UITableViewCellEditingStyleDelete;
}