I have a simple grouped uitableview with a nav bar and an "Edit" nav bar item. the Edit button applies only to the first section, and the other sections of the table do totally different things (think iPhone Settings). My problem is with deleting the last cell of this particular section of the table. Whenever the user deletes the last cell, I am removing the "Edit" button from the nav bar (Cause its not applicable anymore...).
So I do this:
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
.....
if ([section count] == 0) {
self.navigationItem.rightBarButtonItem = nil;
.....
}
.....
}
The problem is that now the other sections are not functional, meaning, I cant click on them anymore and they dont react. I think it's because I did not click on "Done" on the nav bar cause this one was deleted after deleting the last cell, so I am basically still in "editing mode" (not sure if i am right here..).
In any case, I tried to call setEditing:animated: manually after deleting the last cell, but that doesnt help.
For reference:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
NSLog(#"****************** entering setEditing:animated");
[super setEditing:editing animated:animated];
[tblSimpleTable setEditing:editing animated:animated];
}
Any help with this would be much appreciated. Thanks!
Have you tried UITableView* method reloadSections:withRowAnimation:
after
[tblSimpleTable setEditing:editing animated:animated];
?
Works fine on iOS 5. Might be buggy on lower versions.
Related
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.
I have two table view controllers, "root" and "detail". In my root view I have this cell which says "Status". I need to be able to change that. When I click it i am seguewayd to the detailed view which shows the statuses I can pick (static cells). When I click on a status, a checkmark is displayed. But this checkmark also needs to be displayed when a status is previously chosen. So the checkmark should be visible as soon as I am seguawayd in from the root view.
I am able to display a checkmark next to the status as soon as i click a cell. But when I want the checkmark to be displayed when i'm seguawayd, it doesn't show. This is the code im using
- (void) setCheckmarkOnIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self setCheckmarkOnIndexPath:indexPath];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// indexPathForRow set to the third cell to demo
[self setCheckmarkOnIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
}
You need to store the user selection somewhere, and pass it into your controller (or access it in your controller depending on where it's saved and which object owns that information).
Currently, you don't save the user selection so you can't update the cell. Note also that you should be updating the selected cell in viewDidAppear:, not viewDidLoad, and that you should look at using tableView:willDisplayCell:forRowAtIndexPath: to properly deal with the table view being scrolled (and cells being replaced / reused).
If you notice in the iPad contacts app when you tap on the "+" edit icon to add a new address, the cell does not expand to the left when the icon disappears (see 1st screen capture below). I am trying to do something similar but am having no luck (see 2nd screen capture below). I tried using the solution suggested here but didn't work.
For the record I am not trying to replicate the actual contacts app or integrate with contacts. We're working on something unrelated and my designer just wants to follow this pattern so doing some POC work to see what I can do.
EDIT - Another question, to verify my thinking, is that in the contacts app here what's happening is when the "+" icon is tapped, this cell is set to not editing any more, these text fields are added to the cell, the label changed, and then reloadData is called, right?
EDIT - Thanks for the suggestion Tim, I'm almost there! I had actually worked out 1/2 of it but then was running into the issue that the "+" icon was not animating out, just abruptly disappearing. So I added the reloadRows... call to commitEditingStyle but now the entire cell disappears. Here is my code:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCellEditingStyle editingStyle = UITableViewCellEditingStyleNone;
if (self.showEditingIcon) {
editingStyle = UITableViewCellEditingStyleInsert;
self.showEditingIcon = NO;
}
return editingStyle;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// tableView.editing = NO;
// tableView.editing = YES;
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
You can see from my commented-out lines what I had originally, which was working but as I said, was not animating - the "+" button was just abruptly disappearing. If I tried to call [tableView setEditing:NO animated:YES] it would not work. When I tried to put these calls within a UIView animation block it did not look good - could see cell temporarily shift to left and back. However now with the reload call the cell disappears completely. At the moment I'm not making any changes to my cell (for example I'm not adding text fields, etc...) because I just want to make the "+" button animating out without making the cell expand the left work on the first iteration.
Any help greatly appreciated. Thanks.
The trick is to always be in editing mode such that the cell indention remains constant. When the cell is loaded, you decide what state it is in and return the appropriate editing style, either the "insert" style or "none":
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL shouldShowInsertButton = ...;//check internal state for cell
return shouldShowInsertButton ? UITableViewCellEditingStyleInsert : UITableViewCellEditingStyleNone;
}
Then when the edit button is tapped, you update your internal state and reload the cell:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
...;//update internal state for cell
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
EDIT
Here's a working sample project.
I am working with the kind of notes app and I wanted to add the option to swipe-to-delete (like the iPhone's default notes app). I implemented the following two table view methods..
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
//Action to delete value of the cell
}
}
Everything works well except an animation. Showing the delete button on swipe action happens with an animation while hiding it (by tapping or scrolling table view) does not happens with the animation. The delete button was just disappeared immediately.
Can I show the hiding process of the delete button with animation?
For fully control on editing in your custom cell, you should override willTransitionToState method in your UITableViewCell subclass and check state mask
- (void)willTransitionToState:(UITableViewCellStateMask)state
{
NSString *logStr = #"Invoked";
if ((state & UITableViewCellStateShowingEditControlMask)
!= 0) {
// you need to move the controls in left
logStr = [NSString stringWithFormat:#"%#
%#",logStr,#"UITableViewCellStateShowingEditControlMask"];
}
if ((state & UITableViewCellStateShowingDeleteConfirmationMask)
!= 0) {
// you need to hide the controls for the delete button
logStr = [NSString stringWithFormat:#"%#
%#",logStr,#"UITableViewCellStateShowingDeleteConfirmationMask"];
}
NSLog(#"%#",logStr);
[super willTransitionToState:state];
}
EDIT:
Did you try:
setEditing:animated:
Toggles the receiver into and out of editing mode.
(void)setEditing:(BOOL)editing animated:(BOOL)animated
Parameters
editing
YES to enter editing mode, NO to leave it. The default value is NO .
animated
YES to animate the appearance or disappearance of the insertion/deletion control and the reordering control, NO to make the transition immediate.
Discussion
When you call this method with the value of editing set to YES, and the UITableViewCell object is configured to have controls, the cell shows an insertion (green plus) or deletion control (red minus) on the left side of each cell and a reordering control on the right side. This method is called on each visible cell when the setEditing:animated: method of UITableView is invoked. Calling this method with editing set to NO removes the controls from the cell.
Availability
Available in iOS 2.0 and later.
See Also
#property editing
Declared In
UITableViewCell.h
I have a UITableView with custom UITableViewCells.
The table has two sections, the first section has a single row with a UITextField and can only be edited in terms of the text. This section & row cannot be edited from a UITableView perspective
The second section is a list of cells that are generated from an NSArray. These cells are once again custom UITableViewCells comprising of two UITextFields. These cells can be edited from a UITableView perspective, in the sense that the user can delete and insert rows.
In my designated initializer I have specified self.tableView.editing = YES, also I have implemented the method canEditRowAtIndexPath to return YES.
Problem Statement
The table view does not enter editing mode. I do not see the delete buttons or insert buttons against the rows of section 2. What am I missing?
just a suggestion, check whether your controller fit to these requirements :
i use usual UIViewController and it works fine - you need to :
make your controller a delegate of UITableViewDelegate, UITableViewDataSource
implement - (void)setEditing:(BOOL)editing animated:(BOOL)animated
programmatically add EDIT button - self.navigationItem.rightBarButtonItem = self.editButtonItem (if you add EDIT button from builder you will need to call setEditing : YES manually)
Piece of code :)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:YES];
}
- (void)tableView
:(UITableView *)tableView didSelectRowAtIndexPath
:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
// do not forget interface in header file
#interface ContactsController : ViewController<
UITableViewDelegate,
UITableViewDataSource>
Profit!
What if you do [self tableView setEditing:YES animated:YES]; instead of self.tableView.editing = YES;?