DeleteConfirmationButton does not disappear when using custom TableViewCell - ios

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

Related

Slide to delete like android in UITableVIew iOS

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.
}
}

iOS UITableView Swipe Left Buttons don't appear

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

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

how to enable editing mode on UITableViewCell programmatically?

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

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