UITableViewCell: Allowing Selective Deletion - ios

I have a table view and want to allow reordering of all cells, however there are certain cells that I do not want to be allowed to be deleted. when the UiTableView is put into deletion mode I do not want the red '-' button to appear on the left hand side, and do not want the swipe gesture to bring up the Delete button of these cells but want it to happen for the others. Any ideas?

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//if we cant delete the object represented at the index path
if ([[tableViewObjectsArray objectAtIndex:indexPath.row] canBeDeleted] == NO){
return UITableViewCellEditingStyleNone;
}
//otherwise allow the deletion
else{
return UITableViewCellEditingStyleDelete;
}
}
Of course this leaves an empty space where the '-' button should be, but it does not allow deletion. And also does not allow the swipe deletion either.

implement:
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}

Related

IOS: Allow SwipeToDelete But Don't Show Editing Button in Tableview

For a tableview, I want to allow SwipeToDelete and I also want to allow people to edit the tableview in the sense of being able to move rows up and down. However, when moving rows up and down, I want to suppress the delete button as it is a bit distracting.
Is there a way to allow swipe to delete while suppressing the delete button in edit mode?
I am trying the following delegate but it is not doing the job:
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Detemine if it's in editing mode
if (self.editing)
{
NSLog(#"thinks it is editing");
return UITableViewCellEditingStyleDelete; //enable when editing mode is on
}
else {
NSLog(#"thinks editing mode is off");
// return UITableViewCellEditingStyleDelete;
return UITableViewCellEditingStyleNone;
}
}
When I set the else to delete, it allows swipe to delete but also shows delete button.
When I set the else to None, it does not allow swipe to delete but does suppress delete button.
Some how self.editing is not doing the job.
You should return UITableViewCellEditingStyleNone for editingStyle... (to avoid showing the green + or red - buttons at the left of the cell when in edit mode. Then you can return YES from canEdit... to indicate that the cell can still be edited (ie, deleted) by swiping:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
Optionally, you can also use the following to prevent the extra space appearing at the left of the cell during edit mode (where the - or + button would have otherwise been):
- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}

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.

Detect cell click in cell edit showing reordering control + disclosure control as Safari bookmarks editing

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.

Is it possible to remove the rounded deletion indicator when editing a table view?

I have a table view with edit button, and i add the functionality to the table view to reorder cells using the delegate method moverowatindexpath: toindexpath.
But when i tap the edit button it looks like this:
And i want this edit button to use only for reordering.
So can i remove the rounded deletion button when i use the edit button, and only when i swipe i will have the deletion button like this?
I already have the swipe to delete functionality, the only thing i need is that i wNt to remove the deletion functionality when i tap the edit button, and i should rename the edit button to "reorder" :)
Thanks!!
Set these delegate methods:
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
Hope this helps.

Delete when Table Rows are Swap

I have a table. When I swipe horizontally across a table cell a button appears asking if I want to delete the row.
I do not want that feature. However, I looked up the code up and down and I never see where does this feature is implemented.
Other rows on other tables do not behave the same way.
I think what you want to do is:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return NO;
}
in your table's viewController.
However, I looked up the code up and down and I never see where does this feature is implemented.
It's implemented by the table view. You can prevent cells from being edited at all by returning NO from your table data source's implementation of -tableView:canEditRowAtIndexPath:, and you can change what editing options are available for a given row by returning an appropriate value from -tableView:editingStyleForRowAtIndexPath:.
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
// Detemine if it's in editing mode
if (self.editing) {
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
}
or
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}

Resources