Enable edit of tableview with Core Data - ios

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?

Related

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.

UITableViewCell editingAccessory too far right

Today I observed, that my editingAccessory (UITableViewCellAccessoryDetailButton) is too far on the right side, when the my tableView is in editing mode.
When toggling editing on and off I can see the icon move from their regular position to where they are in editing.
In my implementation I'm using
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
EDIT
There is no more code. It's just a plain UITableViewController and the edit button runs noting but the default -setEditing:animated: behind the scenes (as far as I can tell).
EDIT II
Find the complete ViewController implementation here

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

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