How best to store temperature data for different cities? - iphone

It is a little confusing but I will do my best to explain it, I hope it will be fine.
I am trying to create the following functionality in iOS:
I have red button on each row of tableview. The red button is dragged and dropped over the (prototype) tableViewcell and in each row the button will be used. The button text shows the temperature in Fahrenheit. But it changes when tapped so that the color of the button changes to green. And I want the extra functionality at this point; The temperature in Fahrenheit will change into Celcius. (I have the problem on this functionality)
The problem here is that I have a list of tableviewcells (Each is another city), not just one. So I have got that number of buttons and I don't use database to save the temperature data. The data is read through a service and put into an array and transferred to this view by NSUserDefaults.
For example; I have got a list of cities in my tableview. Each is a row and each temperature is put over a red button. Temperature values are the texts of the red buttons.
Berlin 45F
Istanbul 70F
Miami 100F
Chicago 30F
...etc.
So for example, when I tap on the button on Berlin row to change the Fahrenheit to Celcius, there is no problem for the first time so I can get the Celcius value for Berlin, no problem. But when I tap it next time to change it to Fahrenheit again, the value does not come correctly. The Fahrenheit value of Berlin is confused with the other values. Because I don't use a database and it does read the value from an array and it doesn't know which value to use. After some clicks, all the Fahrenheit values come to a single value which is the last city's Fahrenheit value (I use the indexPath.row so it gets the last clicked value)
How can I maintain the temperature data for Berlin without using a database so that when I tap that again after tapping the other cities' buttons, it will show me the correct value (45F or the correct Celcius value)?
Thanks for your help!

While I suggest you use some form of key-based storage (like creating a class "Cities" that stores the temperature, or even storing the city -> temperature key combo in a dictionary), you could read the button text when you click it and convert the value from NSString back to a double or integer.
I assume the function that gets called when you click the button looks something like the one I'm about to write. You can then get the button you clicked and the text it displays like so:
- (IBAction)convertTemperature:(id)sender {
UIButton *myButton = (UIButton *)sender;
NSString *buttonText = [myButton titleForState:UIControlStateNormal];
//...
}
After this, you just convert the string to get the temperature, convert it and display it back.

Related

Collapsable Table Cells [Swift 3] [duplicate]

This question already has answers here:
Accordion table cell - How to dynamically expand/contract uitableviewcell?
(5 answers)
Closed 6 years ago.
I am trying to recreate the collapsable date picker that the calendar app uses when creating a new event. I've put an example of what I'm trying to do on github
In short, I've created a static table, added three cells. The first cell is for the date, and contains a button to toggle the second cell. The second cell is the date picker. The third cell is arbitrary. In the code I'm trying to set the height of the table cell (and the date picker if needed) to zero, and then toggle the size whenever the user clicks the button. No matter what I've tried, I can't a) get the cell to collapse without some sort of gap, and 2) get the animation to smoothly transition from expanded to collapsed and back again.
Edit: This question is not the same as the duplicate answer, in that I wanted to expand a separate table cell and not the same cell as being selected. But, I personally can live with using a same-cell expansion. I also updated my github project so future people can see a working example.
It's very simple; you are probably over-thinking things here. This functionality is built in; Apple wants you to be able to expand and contract a cell. You just aren't using the API Apple has provided. Use it! Here's how.
The date picker cells are always present. But their height is zero (and their clipsToBounds is true) so you don't see them. So implement heightForRowAtIndexPath to return zero for those cells.
To show a date picker cell, change what heightForRowAtIndexPath returns (this is easiest if you have a property that holds this value, so you can just change the property value and have heightForRowAtIndexPath read it from there) and say:
self.tableView.beginUpdates()
self.tableView.endUpdates()
That's all there is to it!
Here's a quick demo I made. The red and orange things are cells. The table has three cells but the second one, containing the date picker, starts out with zero height:

Objective C Collect Data from TextField and List in TextView

On View1, I have an empty textView that will populate with data from a textField. The textField is on View2. Every time I swipe right to view the data in View1, it never lists in the textView. It overwrites whatever is currently there.
Here's my code: https://github.com/dward1289/Objective-CII/tree/master/week4Proj
The assignment is already done, but even after it was graded I never found out what I was doing wrong. Please take a look, and tell me what I can change so that the textField data will populate in the textView and not overwrite the current text.
I guess you want to append the new input to the old information
. If so,
-(void)DidClose:(NSString*)nameString
{
txtView.text = [txtView.text stringByAppendingString:nameString];
}

cycle through entries in NSDictionary

Im writing a game that has two sets of buttons. The first set is like blank spaces, and the second set is a custom keyboard.
So I've created a dictionary to keep track of selected buttons (blank spaces) in my interface. The user can select say 5 of these and update all of their titles at once. When the user presses a key (on my custom keyboard made of buttons in a separate subview) I want to change all of the titles to show the letter on the key the pressed.
I have my keys numbered (alphanumerically) from 1 to 26 (ie. a=1, z=26) and the current set of labels on the blanks will be kept in similar numeric form in an array called "currentSolution"
Im imagining i can set up a for loop to go through selected blanks and give them all the title of whatever key is selected in the keyPressed method. Can anyone give me any guidance here? im kinda a noob and dont really know how i should go about this
thanks
You can use (only in ios 4)-
[yourDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop) {
NSLog(#"dic = %# : %#", key,object);
//Do what you want for every object...
UILabel *label = (UILabel*)object;
label.text = #"bla bla";
}
}];
Hope it will help

Objective C - Create Multiple TextFields for the User, then randomly select one and output the Entered Data

Basically what I am trying to do is:
1) The user sets the number of choices he wants e.g. 3 choices which are "A", "B", "C"
[Done this]
2) The next view is loaded and the right amount of boxes are created, 3 in this case. The boxes need to be blank at first and then the user enters their choices into the boxes.
e.g. "A", "B", "C"
Note: I tried create multiple text boxes automatically, but I found that after about 6 boxes the screen wouldn't scroll and therefore looked very tacky
3)At a click of a button, one of the textboxes is selected randomly. The inputed data from this randomly selected box is then displayed in NSLog or Label or file, which i will then use in another view.
Thanks
Dan
Hey, you could make a scrollView out of the 2th view. That way, you can still automatically create textfields, and the screen will scroll. Check out this link for more information on how to create a scrollview. Perhaps you can make the scrollview's size dynamically (depending on how much textfields you have).
About selecting a random textfield, use arc4random() More about that can be found here.

display 2 picker views in one controller

hi i'm designing a view, where i have to post a query to the bank. For that i take the following input .1)Contact Method : Phone, email or Post(1st Picker) .2) Contact time :Morning, Evening,Afternoon(2nd Picker) 3) Message
now when i select the first text field ie Contact Method, a picker view should display with its datasource. and when i select the contact time text field, it should display the datasource for contact time.
Please tell me how am i to do it
you can create 3 arrays for each variant. Then, create some pointer, for example, currentArray. use this array as dataSource. so the only you have to do - change current array and call [myPicker reloadAllComponents] when your new field becomes firstResponder.