iPhone UIPickerview reloadAllComponents does not work - iphone

I have UIPickerView component that receive data from its datasource (NSMutableArray) which is populated from sqlite.
When user click on button new data row is added to databse.
Now I need to reload UIPickerView to display fresh data from datbase.
I know that I need to use method [storedDataPicker reloadAllComponents]; somehow but I don;t know how :( any direction, help please :)

I had a similar problem. I was loading up an array from a database in viewDidLoad that would populate the components, but I was calling to reload the picker from viewWillAppear meaning that the array that provided the datasource was not reloaded at the same time as the picker and thus not reflecting changed values. Make sure that the original data source is reloading along with the picker.

Related

I have issue of cellForRowAtIndexPath method

I am new at iOS development and i have some issue of cellForRowAtIndexPath method of UITableView.
In my application i have multiple rows in UITableView. when i want to see each row then obviously i need to scroll UITableView.also in UITableView i added UITextField to each UITableViewCell . when i add value
of UITextField in DataBase then its work properly but when i want to edit values of UITextField and i add values of TextField in DataBase then problem is created.
PROBLEM :- If i want to add all editable values of TextField in DataBase then i must need to be scroll UITableView up to down. if i do not scroll then invisible TextField is not add in DataBase :( Here in my Application add and editing are in same UIViewController base on ID that i passed.
by Googling i also found that cellForRowAtIndexPath is not called for all rows. It is only called for visible rows and then called for additional rows as the user scrolls to them. Old rows are also destroyed as they go off the screen and it will request the cells again if the user scrolls back up.
so, please help me how can i solve my problem,
EDIT FOR MORE EASY TO UNDERSTAND MY PROBLEM :
For Example :
I have two UIViewController.
1 - FirstViewController
2 - SecondViewController
in FirstViewController display all list of Name in UITableView and it has two choice either select name in UITableView or add new data by tapp BarButton from UINavigationBar .This both processed adding and editing are done in same ViewController called SecondViewController . data (or values) Add by SecondViewController is working properly
But when i select any row ( or name ) from FirstViewController then related ID pass to SecondViewController and all information are display on TextField base on ID. here all values are get from DATABASE and i store it to NSMutableDictionary and then it store on textField by access KEY of Dictionary. but values are store in TextField which are visible. invisible TextField not store values even i get from DATABASE . so i must need to first scroll UITableView.
Thanks in advance :)
Then implement like given below
Get the database value and store it in NSMutableDictionary and accessible through out by declaring in interface or .h file.
Then when each time your tableView or textField loads read the values from dictionary and display.
And in your textFieldDidEnd or textFieldDidChange delegate get the data from textField & save/store back to your dictionary with the same key. This time it just replaces the old value as you know.
At last just save all the dictionary values into your database on backAction or viewWillDisappear method.
This should work for you, if not let me know.

UITableView Reloading Issue

I am in very complex issue of reloading UITableView!
I've a UITableView in which I am continuously (near by 10seconds), retrieving data from a server and showing that data in my UITableView that contains custom cells. The issue is that, when user (thats me) is scrolling through the UITableView it will crash due to an indexOutOfBound error as I am fetching data from the server the NSMutableArray will clear and fill again with data.
I also tried with a BOOL variable setting YES or NO in scrolling delegates. In this case if I scroll UITableView it will remove the cell of scroll direction, after 10 seconds it will show all cells with data.
But what I want is that, if user scrolls UITableView none of the created cell should hide! scrollEnabled is not a solution for that.
Please help me out!
Thanks :)
this is a thread unsafe code issue. data source being modified while it is being actively used.
one safe solution would be to feed all data after parsing to some DB (sqlite/Coredata) and reload the table datasource from db and call reloadData for table. this way at the time when you were using the datasource array - it wont be empty.
best of luck
if indexOutOfBound means you must check numberofrow that you specified is more than the our array receive in the cell forrowatindex. so check that.

Date Picker And Table View

I'm working on a project on iPhone and I want a Date Picker on a few of the rows in the table view.
Please help me out.
If this is a StaticCell TablieView, then you can just add these via storyboard.
If not, you will need to use the cellForRowAtIndexPath to determine which cell you want to add the datepicker to. You would then instantiate a UIDatePicker, set the attributes if needed and add that subview to your cell.
One warning - using something this large in an iPhone TableView app will result in a crappy user experience.

General understanding about tableview Drawing

I have a generic question about tableview Draw and reload data, and wanted some insight from this scenario. I have a tableView which get's lazily loaded getting the data parsed from a url. Now the concern is when i select a button and move to another view, I can deselect the object from there, which will remove it from the array. Thus, when I go back to the main view of the tableView, it download's the data again and check's if the object of the further view array is there or not, therefore it set it selected the button.
My concern is, when I go back, my previous selected button is highlighted, and then it does all the computation and de-selects it when the data gets loaded. Is there anyway, I can have the tableView redrawn until the data load's everytime?
Thanks.
It Looks like that when you are downloading the data & parsing the downloaded data, you are directly passing the modified variable as a source for the tableview. Instead of it, You may store the source for the tableview into another array, which will get updated from source array if it is downloaded & parsed.
Load the tableview using the secondary array.
This is my understanding. If your problem is not resolved , please provide some piece of code for the problem.
BTW, your problem can be resolved using the above solution.

When is the right time to change the datasource for a UITableViewController

After rendering a UITableView with a given datasource, I want to be able to switch to a different one prior to calling reloadData.
Tried doing it in the method
- (void)viewDidAppear:(BOOL)animated;
but as far as I can see this isn't called when all the visible cells are rendered.
By placing a breakpoint it seems as if nothing is actually rendered. So I must have misunderstood its actual purpose.
Is there a API callback for that or do I have to programmatically look for it?
e.g. in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
when indexPath holds the index for the last rendered cell in the tableview.
To put it into perspective (somewhat).
Suppose we have a UITableView which has a "static" datasource. Once all the static data is displayed, you want to switch to a dynamic datasource which fetches the data off the internet and reloads each row as fetched.
So I'm actually looking for the "right hook" in which to make that change from one datasource to the other.
Are there any other issues one should be concerned about?
Further clarification. I have written all the required code (e.g. an UITableViewController with the static data initialized, different datasources, background threads for the dynamic views, a delegate to notify the UITableViewController as new data is coming in) what I haven't figured out (what the question is all about) is when to actually make the switch from one datasource to the next.
When you call reloadData it forces your UITableView to re-query your data source regardless of the amount of data that is already loaded. Also, the cells that are not visible are not being loaded, so practically the last cell is only filled with data if and when you scroll down to it. If you know for sure that all your cells are visible at once, as you might have a small number of them, you might do as you suggested and call your update function when indexPath holds the index for the last cell in the tableview.
Edit:
In the light of your edits, I'm thinking that perhaps the moment you should switch data sources depends on the state of your "live" data - when you've got enough data in it to be able to show enough of it on the screen. That is, let the static data be displayed, and only switch when you've already fetched a number of values in your live data source.
Another idea might be keeping only one mutable data source, with pre-loaded static initial data. As you fetch new "live" values, you start by replacing the ones that you already have, and then continue adding new values if their number exceeds the initial number of static entries.
viewDidAppear is all about the view controller, not the table. The table reloading will never call that method.
What you want to do, is better handled either via a delegate approach or using notifications. Set up the static data set in viewDidLoad, but then right away start the background dynamic data fetch. When that fetch is complete, have the code downloading the new data call you either as a delegate, or issue a notification your the view controller with the table listens to, and reset the table view data - then you can simply call reloadData, and the cellForRow:AtIndexPath: method will be called again for the rows on the screen (reload data will also revert back to the top of the table).
UITableView load the cells lazy, in other means it only loads the cells that are visible to the user. When the user then scrolls down, the table view will load the new cells while the "old" cells will be released. There are ways to load the entire data source, but this is not recommended from Apple - since it will be heavy on the memory load of the iPhone.
viewDidAppear() is probably your best bet.
The viewWillAppear: and viewDidAppear: methods are only called when your UIViewController gets pushed on the screen (e.g. by an UINavigationContoller or UITableViewController).
What I would do in your situation is to call a method on your own view controller when your dynamic data source is ready (or whenever you intend to change the data source). In this method set the data source and then call [[self tableView] reloadData];
What you should look out for is, if you are constructing your dynamic data source in the background (which I suppose you do) then you need to call your method on the main thread using performSelectorOnMainThread: