Can I use both UITableViewCellEditingStyleInsert and UITableViewCellEditingStyleDelete two button together? - ios

I return canEditRowAtIndexPath is YES. If I swipe left, I can see the delete button and implement my delete action. How can I do if I swipe left, both insert button and delete button appear together?
I know there is a way, I should have a custom cell with two button on the right and just add a panGesture to recognize the swipe action. But I want to use Apple provide for us to do such easy work. Just can see insert and delete button together when I swipe the cell left? My apology for my poor expression.
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(editingStyle==UITableViewCellEditingStyleInsert)
{
NSLog(#"click insert");//I only have delete button, I also want to have insert button .
}
if(editingStyle==UITableViewCellEditingStyleDelete)
{
NSLog(#"click delete");
}
}
I know tableView:editActionsForRowAtIndexPath can do this job … But it is provide for IOS8.0 and later,,How to do for IOS7

At least as of iOS 8 this can't be done (I don't know if this changed in iOS 9). A cell can only have one of the 3 possible editing styles - None, Insert, or Delete. UITableViewCell does not support both Insert and Delete at the same time. When the table is in editing mode, the cell will show either no button, an insert icon on the left, or the delete icon on the left based on the row's single editing style.
It actually makes no sense to have both insert and delete on the same row. What does that mean for the user? Normally a table view would only have a single row (per section) that might have the insert editing style. This row, when tapped, allows the user to add a new row to the table view. Other existing rows might have the delete editing style to allow the user to delete that specific row.
So again, what would it mean for a single row to have both insert and delete editing styles?

Related

How to customize the Delete button shown when swipe a UITableViewCell

I have a table view and enabled the swipe to delete feature by providing an empty implementation of - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath of the table view controller.
I can see a red 'Delete' button when I swipe left the table cells.
But the button's label is 'Delete' despite that the system locale is not English.
I have too problems.
1. I want the Delete button to be localized according to the locale.
2. I want to add some other button, like that in the Podcast app of Apple.
there is too much solutions... you should to search before posting new question. check this link:
iOS Programming 101: How To Create Swipeable Table View Cell to Display More Options
or use ready-made solutions like this:
https://github.com/CEWendel/SWTableViewCell
i can't post much links without 10 reputation, so try to google "custom delete button uitableviewcell"

customising cell editing style in uitableview in ios

I want to give two options when user swipe right direction.
The two option or button with name edit and delete.
At present it is show only delete when user swipe left i.e UITableViewCellEditingStyleDelete
But i want to customize this view and give two button with gray background.At present it is red background.
The code is :-
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
Please help me how to acheive this.
You need to subclass UITableViewCell and override
setEditing:animated: and
layoutSubviews
This should work in the expected way.
The button is not customisable, if you want to do that behaviour you'll have to create your own custom cells and add the animations yourself.

Restrict dragging onto specific section

I want to restrict dragging option onto specific section in a single table view. Now I enabled dragging option onto just single section in specific section but what happened when use move successfully cell onto other section where dragging not enabled so it shouldn't move to that section where dragging is not enabled. Kindly see attached image where I enable editing for deleted in first section and dragging for section section and row shouldn't able to move onto first section. I want to perform this in single cell. Is this possible if yes then how?
This will be great for me. Thanks in advance.
Use targetIndexPathForMoveFromRowAtIndexPath::. It's a UITableViewDelegate method that will allow you to intercept the index path that the user is attempting to drag the cell to. If the section is somewhere that you don't want them to be able to drag the cell, simply return the source index path, and upon release, the cell will bounce back to its original position.
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
if (proposedDestinationIndexPath.section == sectionYouDontWant) {
return sourceIndexPath;
}
return proposedDestinationIndexPath;
}

How to know that no row is selected in my UITableView?

I use a UITableView to display a list of items, and when a row is selected I show that item in a detail view next to it (using a split view controller).
As the selection in the table view changes I want my detail view to change as well, which is of course standard behavior and it works flawlessly... except when no row is selected anymore in the table view.
Users can for example delete a row from the table view, and during and also after that deletion there will be no selected row in the table view. Of course after a row is deleted I let my detail view controller know, but during the editing process in the table view, when no row is selected, my detail view controller does not know this.
I tried to use the UITableViewSelectionDidChangeNotification but that only gets sent when the user selects a different row, not when a deselection occurs.
How can I be notified of the fact that the table view switches from having a row selected to having no selection at all?
EDIT:
As a temporary solution, I tried renaming the Edit-button to "Reorder" and only allowing moving rows from A to B but without allowing the deletion controls, but this can not be done, there is no move control without enabling editing on a row. The thing is, I do get a pointer to a row that is up for moving, so I can keep that selected. I do not get this pointer for a row up for deletion, so no go for me. I always want to know what row a user works on, and keep that row selected at all times.
I may have to resort to ditching the standard editing behavior and adding my own buttons and methods for it. Or see what gestures can do for me to capture touches on any row...
There is a property indexPathForSelectedRow. I'm not sure what it would return when there is no selected row, but it may return nil or something like that, which you could use as a trigger to know if there are any rows selected. Something like
if (myTable.indexPathForSelectedRow == nil)
{
//do something here to account for no rows being selected
}
else
{
//do stuff here to set up your detail view
}
Let me know if this works for you.
How are you implementing the deletion of rows? Are you implementing the following data source method?
tableView:commitEditingStyle:forRowAtIndexPath:
You can have a property like
NSIndexPath *selectedIndexPath
and set it to nil when a row is being deleted.
Example:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//select another row in your table
[tableView selectRowAtIndexPath:*indexPathOfTheRowToBeSelected* animated:YES scrollPosition:UITableViewScrollPositionNone];
//then delete row from data source
}
The method above will be called when the user touches the delete button on the table he/she has selected to delete.
Okay I misread what you were asking at first, I'm pretty sure what you want is either – tableView:willDeselectRowAtIndexPath: or – tableView:didDeselectRowAtIndexPath:. These methods will be triggered automatically when the user deselects a row (either right before the row is deselected or right after, respectively). Implement one of these in your table view delegates code, and you'll know whenever a row gets deselected, and you can put your code to select the next row or whatever you want in one of these methods.
I struggled with this as well. Turns out that when the "Edit" button is hit, all rows are deselected at that point. You can override setEditing:animated: to detect when it is hit, but you must call [super setEditing:editing animated:animated] before you return.
See this link for where I found this:
How can i identify self.editButtonItem button's clicked event?

Changing UITableView functionality on button press

I'm trying to figure out the best way to do this - I have a UITableView of items which the user has previously selected and which is stored. When you click an item it takes you to a detail page. What I need is to be able to click a button below the table view which reloads the table and changes the accessory so its a tick instead of a disclosure, then the user can un-tick the items and remove them from the list, before clicking another button which reloads the updated table and restores the disclosure accessory.
Question is, what is the best way to "remember" which way to handle the table reload after the click so it knows which way to display it? Would you use the NSUserDefaults to store a flag on the button click or is there a more elegant way to do it? I guess I could use the status of the button, whether it's in one state or another, but I'm guessing there is something in-built I'm missing.
Hope that makes sense - thanks.
Usually I have some 'model object' and depending on array of those objects I construct table. My cellForRowAtIndexPath looks like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
InterestsCell *cell = (InterestsCell*)[tableView dequeueReusableCellWithIdentifier:kInterestsCellID];
if (cell == nil) {
...
}
Interest *i = (Interest*)[self.interestsArray objectAtIndex:indexPath.row];
cell.myTfSubview.enabled = i.isChecked;
return cell;
}
If you don't have your model then you can create array of BOOL values and store flags there.

Resources