UITableView editing not working - iphone

I have a button (a UINavigationBarItem) used for editing my UITableView, which only allows deletions. So when I press delete, the little red line comes up next to each cell, and I can delete each row.
When the button is pressed, the following function is called:
-(void)editButtonSelected:(id)sender {
if(self.editing)
NSLog(#"self.editing = true");
else
NSLog(#"self.editing = false");
if(self.editing) {
[super setEditing:NO animated:NO];
[tableView setEditing:NO animated:NO];
[tableView reloadData];
[leftButton setTitle:#"Delete"];
[leftButton setStyle:UIBarButtonItemStylePlain];
self.editing = false;
}
else {
[super setEditing:YES animated:YES];
[tableView setEditing:YES animated:YES];
[tableView reloadData];
[leftButton setTitle:#"Done"];
[leftButton setStyle:UIBarButtonItemStyleDone];
self.editing = true;
}
}
And it works fine. But only for a while. As soon as I introduce a new UIViewController, and then dismiss that controller, this delete function doesn't work on this main screen I have. It works fine until a new UIViewController is put on top. The button itself works fine, and the value of self.editing does get toggled between true and false correctly, but the little red lines do not show up. Why could this be happening?

I would guess that the target on your UINavigationBarItem is still set to the first view controller, not the subsequent view controllers that get pushed on the stack.

Related

editingStyleForRowAtIndexPath -- fires in iOS 5, does not fire in iOS 6

I have simple master and detail view controllers connected with two segues, one for "show detail" and one for "add new".
"Show detail" segues to the detail view controller with setEditing:NO.
Tap "+" (add icon) segues to the detail view controller with setEditing:YES
iOS 5.1: "+" works as I expect, the detail page is in edit mode and editingStyleForRowAtIndexPath fires to show the insert and delete indicators.
iOS 6.0: the "+" makes the transition to the detail page but editingStyleForRowAtIndexPath never fires. Other code that is in setEditing:YES gets executed. didSelectRowAtIndexPath does fire (delegate = self).
Once on the detail page edit mode works as expected in both cases.
Any ideas?
// Master.m
if([[segue identifier] isEqualToString:#"NewRecipe"]) {
DetailViewController *detailViewController = [segue destinationViewController];
// stuff
detailViewController.recipe = r;
detailViewController.delegate = self;
detailViewController.editing = YES;
}
// Detail.m
-(void)setEditing:(BOOL)flag animated:(BOOL)animated {
if (flag) {
[self.tableView setEditing:flag animated:YES];
[self.tableView beginUpdates];
// the row does get added
[self.tableView insertRowsAtIndexPaths:#[pathToAdd] withRowAnimation:UITableViewRowAnimationAutomatic];
// datasource gets updated here
[self.tableView endUpdates];
....
}
}
I figured it out. I don't know why this is the fix, I hope this does not replace bad code with worse code.
Master.m
// iOS 5 -- this is OK
detailViewController.editing = YES;
For iOS 6 I needed the detailViewController to make a call to a delegate method to determine whether to setEditing:YES.
Master.m
-(BOOL)isNewRecipe {
return (_isNewRecipe == 1);
}
Detail.m
if ([self.delegate isNewRecipe]) {
[self setEditing:YES];
}

Activity Indicator not starting animation straight away

I initialize an activity indicator and in a button press action I start it animating and call the next view to display.
-(IBAction) downloadButtonPressed:(id)sender {
NSLog(#"Download Button Pressed");
indicator.hidden = NO;
[indicator startAnimating];
if (addviewcontroller == nil)
addviewcontroller = [[AddViewController alloc]init];
[self.view addSubview:addviewcontroller.view];
[addviewcontroller setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:addviewcontroller animated:YES];
}
When I press the button, the activity indicator doesn't start immediately. It starts when the other view is called. The indicator is displayed for a second, but when the button is pressed it takes some time to load the other view.
I dont know why the indicator shows for a second without starting.
Try this :
-(IBAction) downloadButtonPressed:(id)sender
{
NSLog(#"Download Button Pressed");
indicator.hidden = NO;
[indicator startAnimating];
[self performSelector:#selector(showController) withObject:nil afterDelay:0.1f];
}
- (void)showController {
if (addviewcontroller == nil)
addviewcontroller = [[AddViewController alloc]init];
[self.view addSubview:addviewcontroller.view];
[addviewcontroller setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:addviewcontroller animated:YES];
}
That should do the trick ;-)
EDIT
I just noticed that there is a problem in your code, you are adding your addviewcontroller twice. One by adding it as a subview of the actual view controller, and one by modally presenting another view controller. You should remove one of the statements from this function.

animation issues when editing a uitableview

This is really simple, though still driving my nuts. I have a uitableview where I am trying to animate transition in and out of editing mode. This is what I took from an example that I have seen. It does do the job, but without the animation.
Any thoughts?
- (IBAction) EditTable:(id)sender
{
if(self.editing)
{
[super setEditing:NO animated:YES];
[tblSimpleTable setEditing:NO animated:YES];
[tblSimpleTable reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Edit"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
[super setEditing:YES animated:YES];
[tblSimpleTable setEditing:YES animated:YES];
[tblSimpleTable reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Done"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
}
}
PS: I am also not sure why I need this line: [super setEditing:NO animated:YES]; but things just dont seem to work at all without it. I just saw a few examples online that dont do that.
Thanks!
Maybe you should not reloadData when set editing property.
BTW, What's your "super" class? Normally you don't have to invoke [super setEditing:YES animated:YES];
Is it only the button that isn't animating properly? Either way you should probably be using super.editButtonItem instead of your own; it's animated and just setting the text and style like that (I believe) isn't. As far as calling the super, are you overriding one of the editing methods and not calling the super method from within there? And xuzhes's answer about the reloadData is, I believe, correct as well.
Try this:
#Implementation YourViewController // This can (should) be a subclass of UITableViewController to make your life easier
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem; // Automatically calls setEditing:animated: and changes itself to "Edit"/"Done" between presses
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
if (editing == YES) {
// Do stuff here
} else {
// Do stuff here
}
// Reload all sections of the table view
// THIS IS THE PART YOU'RE INTERESTED IN
NSRange range = NSMakeRange(0,[self.tableView numberOfSections]);
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
}
Check out the documentation for comments on the methods from Apple :)

how to change barbuttonitem from edit to done

i am placing a toolbar at the top of my view with edit button.
For edit button i am giving action like edit the table as shown below.
- (IBAction)editModeForTable {
[tableview setEditing:YES animated:YES];
NSLog(#"edit button clicked ");
}
Now what i need is when i click on edit button i need to change this edit button to done button.
this is myscreen.
when ever i click on edit i need to set edit mode for table and change edit button to done.
similarly when ever i click on done button i need to change that done to edit button and edit mode should be false.
Update:
- (IBAction)editModeForTable {
if (buttonClickid == 1) {
[allLIsts setEditing:YES animated:YES];
mybutton.style = UIBarButtonSystemItemDone;
mybutton.title = #"Done";
buttonClickid = 2;
NSLog(#"mmm");
}
if (buttonClickid == 2) {
[allLIsts setEditing:NO animated:YES];
mybutton.style = UIBarButtonSystemItemEdit;
mybutton.title = #"Edit";
buttonClickid = 1;
NSLog(#"ppp");
}
NSLog(#"edit button clicked ");
}
this is button action where buttonclickid is int.
it executes both conditions why?
[btn setStyle:UIBarButtonItemStyleDone];
[btn setTitle:#"Done"];
Similiar to revert back.
btn is an IBOutlet connected to the button in Interface Builder, or your created UIBarButtonItem.
Your if logic is broken.
- (IBAction)editModeForTable {
if (![allLIsts isEditing]) {
[allLIsts setEditing:YES animated:YES];
mybutton.style = UIBarButtonSystemItemDone;
mybutton.title = #"Done";
NSLog(#"mmm");
}
else {
[allLIsts setEditing:NO animated:YES];
mybutton.style = UIBarButtonSystemItemEdit;
mybutton.title = #"Edit";
NSLog(#"ppp");
}
NSLog(#"edit button clicked ");
}

iPad not resigning responder

I have a table with 3 UITextFields added to the content views of cells (1 row per section). I have a 4th section that I insert so that I can scroll the particular field to be above the keyboard.
The problem I have (on the iPad only) is that when one of the text fields has firstResponder, it does not relinquish it when user taps on another field. Are there differences in the responder chain on an ipad? In the bellow code for my view controllers UITextFieldDelegate - textFieldShouldEndEditing does not get called when touching another field. Pressing the done button works as expected (unless another field has been touched).
Anything wrong with the code bellow?
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (!_editing++) {
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:4] withRowAnimation:UITableViewRowAnimationNone];
}
// scroll to section number in the text fields tag
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:textField.tag] atScrollPosition:UITableViewScrollPositionTop animated:YES];
return YES;
}
- (void) textFieldDidBeginEditing:(UITextField *)textField {
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:textField action:#selector(resignFirstResponder)] autorelease];
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
if (_editing-- == 1) {
//
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:4] withRowAnimation:UITableViewRowAnimationNone];
}
self.navigationItem.rightBarButtonItem = nil;
// do something with the text here...
return YES;
}
OK, i have found a satisfactory workaround, it seems allowing the run loop to execute before the request to animate removing a section fixes the problem. I guess this is an apple bug? For anyone else worried about this issue - i was running iOS3.2.1 on the iPad.
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
if (_editing == 1) {
[self performSelector:#selector(hideFinalSection) withObject:nil afterDelay:0.0f];
}
else {
_editing--;
}
self.navigationItem.rightBarButtonItem = nil;
// do something with the text here...
return YES;
}
- (void) hideFinalSection {
if (!(--_editing)) {
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:4] withRowAnimation:UITableViewRowAnimationNone];
}
}