How to change the values of an array and then view - ios

I want to subtract 273 from the received values of array to view the temperature in Celsius.
cell.textLabel.text = [NSString stringWithFormat:#"Kelvin: %#",[day objectAtIndex:indexPath.row]];
Here day is an array of values from which I want to substract value 273

you can do this in two ways
in your cellforRowAtIndex
cell.textLabel.text = [NSString stringWithFormat:#"Kelvin: %d",[[day objectAtIndex:indexPath.row] intValue] - 273];
where u get the response on that place
assume that yourstr=#"345";
NSString *cal = [NSString stringWithFormat:#"%d", [yourstr intValue]-273];
add to ur array
[day add object: cal];
finally u show the normally in your tableview
cell.textLabel.text = [NSString stringWithFormat:#"Kelvin: %#",[day objectAtIndex:indexPath.row]];

cell.textLabel.text = [NSString stringWithFormat:#"Kelvin: %d",[[day objectAtIndex:indexPath.row] intValue] - 273];
Give it a try

Related

Count objects in array for string

I have an array stored in Parse and I wanted to count the objects in the array and then display that number in a label. I have been able to count the objects but for some reason can not figure out how to turn it into a string. Here is my code:
cell.CheckinLabel.text = (#"count = %d", [[imageObject objectForKey:#"Checkin"] count]);
NSLog(#"count = %d", [[imageObject objectForKey:#"Checkin"] count]);
Any help would be appreciated.
Try this:
NSString *string = [NSString stringWithFormat:#"%d", [[imageObject objectForKey:#"Checkin"] count]];
yourlabel.text = string;
Or,
yourlabel.text = [NSString stringWithFormat:#"%d", [[imageObject objectForKey:#"Checkin"] count]];

iOS NSArray count

I think my question is simple but it stumped me. Basically I have a NSArray of objects for example:
NSArray *arrayT = [NSArray arrayWithObjects:#"A", #"B", #"C", #"D", nil];
I want to put these objects into a TableViewCell. To display, I did
cell.positioninarray.text = [arrayT objectAtIndex:indexPath.row];
cell.letterinarray.text = [arrayT objectAtIndex:indexPath.row];
I am expecting this result
1 A
2 B
3 C
4 D
But am getting
A A
B B
C C
D D
I've looked through the NSArray help file in Apple developer website, but i don't see one that works for me.
Please help! Thanks!
cell.positioninarray.text = [NSString stringWithFormat:#"%d", (int)(indexPath.row+1)];
Replace
cell.positioninarray.text = [arrayT objectAtIndex:indexPath.row];
with
cell.positioninarray.text = [NSString stringWithFormat#"%d", ((int)indexPath.row+1)];
Explanation
You are setting the text for both positioninarray and letterinarray to be
[arrayT objectAtIndex:indexPath.row];
So you are getting A, B, C, D in both of the labels. To get 1, 2, 3, 4 in positioninarray, use the above code.
Change your code as below, indexPath.row will give you current index for the cell, and you want position start by 1 not 0 so indexPath.row + 1 will do your work, and finally convert your int value to string using NSString stringWithFormat :
cell.positioninarray.text = [NSString stringWithFormat:#"%i", (indexPath.row + 1)];
cell.letterinarray.text = [arrayT objectAtIndex:indexPath.row];

UITableView cell subtitles from plist data

I want to add some subtitles to my UITableView cells (where I display continents, countries, other data). Already populated my first table with continents. Now I want to add numbers of countries as subtitle in every continent cell. I added:
int numberEurope;
numberEurope = [europe count]; //this works fine
int numberAfrica;
numberAfrica = [africa count]; //this works fine
NSNumber *myNum1 = [NSNumber numberWithInt:numberEurope];
NSNumber *myNum2 = [NSNumber numberWithInt:numberAfrica];
NSArray *myArray = [NSArray arrayWithObjects: myNum1, myNum2, nil];
cell.textLabel.text = ContinentName;
cell.detailTextLabel.text = [[NSString alloc] initWithFormat:#"%# countries", myArray];
return cell;
But the subtitles are the same in every cell, the full array is shown: ( 2, 2) countries instead of 2 countries in the first cell and 2 countries in the second one. What am I doing wrong? The plist I use is screenshot here: Cannot Feed UITableView with .plist
You have to select proper value based on indexPath:
cell.detailTextLabel.text = [[NSString alloc] initWithFormat:#"%d countries", [[myArray objectAtIndex:[indexPath row]] intValue]];

Table Views with plists

I have a plist with the structure:
Root - Dictionary
Notes - Array
Item 0 - Dictionary
Title - String
Text - String
Date - String
I am then doing:
...
NSString *noteTitle;
NSString *noteText;
NSString *noteDate;
self.notes = [self.data objectForKey:#"Notes"];
And configure the cell like this:
cell.textLabel.text = [[self.notes objectAtIndex:indexPath.row] objectForKey:#"Title"];
cell.detailTextLabel.text = [[self.notes objectAtIndex:indexPath.row] objectForKey:#"Date"];
How do I then, in a button, add the noteTitle to the "Title" value in the plist?
[self.notes addObject:noteTitle];
When the alert is shown you need to know the index of the table you're editing so you know what index to edit when the button is tapped (row below). Then, you'll be doing something like:
[[self.notes objectAtIndex:row] setObject:noteTitle forKey:#"Title"];
If you really want to add a whole new item then you should be creating a new mutable dictionary, adding noteTitle to it and then adding that dictionary to self.notes (in which case you don't need the row).
NSDictionary *d = [[NSDictionary alloc] initWithObjectsAndKeys:noteTitle, #"Title",noteText, #"Text",noteDate, #"Date", nil];
[self.notes addObject:d];
if you want to fold 2 strings:
NSString *newString=[NSString stringWithFormat:#"%#%#",oneString,secondString];

iOS - key index from 3 strings

I am storing items in a large NSMutableArray. 3 strings define a unique item in this array. I'd like to have a NSMutableDictionary which maps the 3 string key to an entry in the array.
In my code the first 3 objects in item are the 3 strings which define a unique item. How can I most efficiently create the key to do the lookup? I'm guessing stringWithFormat isn't the best idea. I'm trying to speed up a large amount of lookups that occur.
- (void)addItem:(NSArray*)item {
// create entry from item
[mEntries addObject:entry];
NSString *key = [NSString stringWithFormat:#"%#%#%#", [item objectAtIndex:0],
[item objectAtIndex:1],[item objectAtIndex:2]];
[mEntryMap setObject:entry forKey:key];
}
- (Entry*)getItem:(NSArray*)strs {
NSString *key = [NSString stringWithFormat:#"%#%#%#", [strs objectAtIndex:0],
[strs objectAtIndex:1],[strs objectAtIndex:2]];
return [mEntryMap objectForKey:key];
}
Create string directly from array no need to use long method
- (void)addItem:(NSArray*)item {
// create entry from item
[mEntries addObject:entry];
NSString *key=[item componentsJoinedByString:#""];
[mEntryMap setObject:entry forKey:key];
}
- (Entry*)getItem:(NSArray*)strs {
NSString *key=[strs componentsJoinedByString:#""];
return [mEntryMap objectForKey:key];
}

Resources