why does my UITableView not appear to start in editing = YES mode when initialized? - iphone

I have a UITableViewController subclass. I set self.editing = YES at the end of the viewDidLoad method, but when the table is displayed the little red 'delete' icon does not appear next to each row.
Then, I added an edit button to the navigation item:
self.navigationItem.rightBarButtonItem = self.editButtonItem;
When I launch the application now, it starts in edit mode (I can tell because the edit button says 'done') but, again, the delete icons don't appear. Then if I toggle it with the edit button twice (once to turn it off, then once to turn it on), the red delete icons appear.
So, my question is: why aren't the table cells displaying correctly when first displayed? I've tried moving the self.editing = YES line to other places in the code, like in the init function or the viewWillAppear function, but no dice. It seems like this is a result of funny ordering somewhere (e.g. table cells initialized before editing is set, or something), but I can't figure it out; running the debugger shows that the viewDidLoad call happens before cellForRowAtIndexPath, as one would expect.
Other notes:
Yes, I am having editingStyleForRowAtIndexPath return UITableViewCellEditingStyleDelete. But from debugging I've verified that that function is not even called after the first load. (It gets called after I toggle editing mode twice.)

d'oh, adding it in viewDidAppear (or, better: viewWillAppear) did the trick. Thanks #lukya and #willcodejavaforfood for suggestions.
I feel silly. I swear I thought I'd checked that.
Moral of the story: setting the editing property too early makes page elements not display in editing mode. (Doesn't that seem like a bug?)

Try using the
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
method in UITableView

Related

iPhone programming-- UIView elements appear to be ignoring Hidden=NO calls

Using XCode 4.4 and Mountain Lion,
I have a UIImageView, and on that view, I have a UIProgressView and a UIButton. When the app starts, the progress view and button are both hidden, as set in the storyboard. I then try to unhide them, first the progress bar when I'm doing something, and then the button when I'm done. I have called, for both items,
[self.view bringSubviewToFront:saveToCameraRoll];
to try to put them in front of the UIView.
Problem is, when I programmatically try to unhide them, it doesn't work. I can call:
progressBar.hidden = NO;
[self.view bringSubviewToFront:progressBar];
And that does nothing.
So then I tried to set everything as visibile in the storyboard and then programmatically set everything to be invisible once the controller loads. No deal; now my calls to hidden = YES seem to be ignored.
I then removed all actual programming, so that hitting buttons should just cause the button and progress bar to appear, reasoning that maybe the main thread was getting starved and couldn't update.
How can I force elements to pay attention to being hidden?
EDIT: I've now also tried programmatically modifying the alpha from 1 to 0. No change. It's like everything I'm doing is getting ignored. I made these items via the ctrl-drag method into the #interface section of the .m file; maybe I don't have some more delegates or interfaces or whatever hooked up?
As you said that you connected the ivars with your XIB file, it seems like your problem is that you are doing stuff on the main thread which in return blocks the run loop and your UI doesn't get redrawn anymore. When you update UI elements by changing their properties, those changes aren't applied instantaneous but the next time the UI gets redrawn which only happens when you give the main threads runloop a chance to run. However, if you do something like the following code, the changes will never appear:
[button setHidden:YES];
[self doSomethingReallyExpensiveAndTimeConsuming];
[button setHidden:NO];
The result of this code is that the button is set to be hidden, but doesn't get redrawn because the system has no chance to do it and when the system actually has a chance to redraw the UI, the button is already set to be invisible. A fix for this is to either split the work up and schedule it via timers on the main thread, or to use something like GCD to offload the work on a secondary thread (but then you need to make sure that your code is threadsafe!)
Is it possible the outlets are nil because they aren't hooked up in IB? If so, no amount of manipulation will have an effect.
Try NSLog(#"%#", saveToCameraRoll);
Is it null? Fix by reconnecting outlets in IB. If that works, then .hidden = NO will work and you can get rid of any code you added to manipulate the view hierarchy.

Trying to make a Custom UITableViewCell with a UITextField becomeFirstResponder when Editing

I have a UITableViewController with custom UITableViewCells that contain a UITextField. When switching the table view into edit mode, I add a new cell to the bottom of the table and would like to make this cell becomeFirstResponder. My tableView:cellForRowAtIndexPath: method checks for this bottom cell, so I just added the line:
[cell.theTextField becomeFirstResponder];
Which I believed should work. However, when the table view is first displayed, it does not seem to be working. BUT if I select the cell (making it the first responder), then go out of edit mode (causing a resignFirstResponder within my code), I can then go back into edit mode and magically it becomes the first responder as I would expect!
Note that even if I end editing mode with a different cell selected (they all have text fields) and go out of edit mode, then back in, it still works, as long as at some point I had made the last cell becomeFirstResponder (by selecting it).
So, my guess is that when it first becomes the first responder, there is something getting set either in the table view or some place else that wasn't originally set, and from then on it makes this work.
Anyone have any ideas as to what may be going on here?
The first becomeFirstResponder call fails because the cell doesn't have a superview yet. The table view adds the cell as its subview after you return it from tableView:cellForRowAtIndexPath:. I suggest you make this call somewhere else. If you already have a custom UITableViewCell subclass, you could implement didMoveToWindow: and call [self becomeFirstResponder] there.

Reloading / Displaying searchResultsTableView of UISearchDisplayController after search method iteration is finished

I have modified the code of Apple's sample iOS project TableSearch in order to use it for searching a webservice by parsing its contents. Everything I have implemented works fine except for one ugly detail when performing a search using the SearchDisplayController's SearchBar. I have changed the behaviour of the SearchDisplayController to make it call my search function first when the "Search" button was tapped.
The problem is that when the search iteration (which is performed in the background in an NSOperationQueue) is finished the "searchResultsTableView" (of the searchDisplayController) is not automatically displayed or not assigned the resulting content. If you then change the SearchBar's text or tap the "Cancel" button from the view which appears when you touch the search field (see TableSearch) the correct TableView appears with the search results. I simply want to have this step to be executed right after the search operation is finished, so before you interact. At this stage the "No Results" label is currently displayed. The methods "filterContentForSearchText" and "shouldReloadTableForSearchString" are unchanged from the original TableSearch project.
I have taken a look at different class references of the SearchDisplayController and its attributes but I could not find any final solution yet.
I have tried the following in a section which is definitely iterated after the NSOperation is finished but it does not seem to solve the problem.
[self.searchDisplayController.searchResultsTableView removeFromSuperview];
and
self.searchDisplayController.searchResultsTableView.hidden = YES;
These operations both have the correct view I want displayed BUT scrolling is disabled until you change the state so that the view is hidden again. It is possible to select TableView cells, though. I basically want to have this just with scrolling enabled...
Thanks in advance for your effort!
I have same problem and I just solved it. I had exactly same problem, I wanted disable instant search and when I hit search button, the table didn't load but when I clicked cancel, it loaded up. And If I scroll on table view that didn't load correct result after search, it crashed due to index out of bound.
The thing you need to do is reload searchResultTableView not current tableview. After you filter out your data by search term, put
[self.searchDisplayController.searchResultsTableView reloadData]
to reload your search result, and it will shows up after you hit search button.
Hope this help
I found that setting the searchBar.text causes searchDisplayController.searchResultsTableView be added to self.view, I solve it like this:
self.searchBar.text = #"xxxx";
[self.searchDisplayController.searchResultsTableView removeFromSuperview];

refreshing my applicationData ( actually my Title )

I seem to be having a problem with the titles in my application.
Let me describe what is happening here.
1) starts out good with the correct image
http://img262.imageshack.us/img262/5808/picture15b.png
2) still good after clicking a cell
http://img31.imageshack.us/img31/4440/picture13dvg.png
3) after hitting the backbutton previous title remains and the last one is gone
http://img150.imageshack.us/img150/5689/picture14ltq.png
4) however after scrolling up and down fast enough the correct view appears again
http://img262.imageshack.us/img262/5808/picture15b.png
As you can see in the images above I'm using a UITableview however I'm NOT using the navigationItem title , I'm using a UILabel ( called title ).
When I click on my cells and scroll through my application everything is fine however when I click my backbutton, which is also not a standard backbutton from the tableview, but a UIBarbuttonItem it just pops the last view (and does some other stuff too)( the button below as seen in my imagelinks).
-(void)back_clicked:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
....
}
It appears that my Titles do not refresh when I press the back button(which makes sense I guess since i do not have some sort of a refresh function ).
I concluded this because when I scroll down really fast my original title comes back.
I noticed but could be wrong , bear with me here , that reloadData applies mostly to table ,cell constructing and such so I assume this is not the one I'm looking for.
So is there some sort of refresh function I can use ?
I would really like some suggestions ;)
thnx all for reading
If when you h it the back button you are coming back to another viewController i would suggest looking at the method viewWillAppear from UIViewController, you can override this method and call it before you go back, in the method implementation you can set the correct title ...reference http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/viewWillAppear:
How are you inserting the UILabel that you use as a title?
Like this: [self.navigationController.navigationBar.titleView = < label >];
or more like this: [< top window > addSubview: < label >];
I've never had any problems with custom title views using the first method.

Selecting a UITableViewCell in Edit mode

If I create a UITableViewController - drilling down works as expected. If a user selects a cell and I implement 'didSelectRowAtIndexPath', the cell flashes blue and the next view shows up.
But, if I include an 'edit' button (self.navigationItem.rightBarButtonItem = self.editButtonItem), when the user clicks 'Edit' - the mode correctly changes (all the cells indent and paint an appropriate editingAccessory), ... BUT, the cells are no long 'selectable'.
IE: while in edit mode, when a user selects a cell, nothing is happening. No blue flash. No invocation of 'didSelectRowAtIndexPath', nothing.
When I open the iPhone example 'iPhoneCoreDataRecipes' (as provided in the SDK docs), sure enough, they have a RecipeDetailViewController - that, when put into edit mode, still allows you to drill down. I've downloaded and built their example and it works just fine. I can't seem to find any trickery in their code to enable this 'selectable cell when in edit mode behavior' but I'm just not getting it when I do it.
Thoughts?
Thanks for any time,
-Luther
UITableView has a property allowsSelectionDuringEditing for this.
IMHO it should have been YES by default.
You have to set the allowsSelectionDuringEditing property of UITableView to YES.
The default value is NO.
It can be changed in Storyboard: