Delete when Table Rows are Swap - ios

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

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

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.

Enable edit of tableview with Core Data

I would like to make a table view editable, i.e. you click edit and it gives you the option to delete entries. My understanding is that Apple makes this easy with the following code:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
I had this working when my table was in a mutable array, however since I shifted to Core Data and NSFetchedResults controller, while it shows the edit button on the upper left and also gives you a done button after you click edit, the table is not editable.
Is something special needed for Core Data or what could be wrong?

Swipe cell to delete show two buttons but need one

I have TableView with a custom UITableViewCells, and when i swipe cell, to show button, i get two Buttons like this -
But need one button - "Delete"...
What i do wrong?
Cell is Custom...but i do not add gray button.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath{
// Return NO if you do not want the specified item to be editable.
return YES;}
I understood. I did't see, what i added not documented function, which are for second button -
-(NSString *)tableView:(UITableView *)tableView titleForSwipeAccessoryButtonForRowAtIndexPath:(NSIndexPath *)indexPath;{
return #"";}
You just need to return UITableViewCellEditingStyleDelete in the delegate method:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}

UITableviewcell delete button apprearing when I swipe right to left on a cell

I have UITableview and I dont need to edit or delete the cells. So requirement is just view only. But when I swipe on a cell a red delete button appears. Please help how can I disable it.
You have a few options depending on your needs. If you don't need any cell editing of any kind then simply add this method:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
Editing defaults to YES so you need to turn it off.
IF you need some editing but no deletion then implement:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
Also, if you don't need either insert or delete, remove the tableView:commitEditingStyle:forRowAtIndexPath: method that may have been added automatically.
Add the following method to your UITableViewDataSource implementation:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}

Resources