Table Views with plists - ios

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];

Related

How to change the values of an array and then view

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

select indexOfObject from NSArray in Xcode by comparing string

Hi I have NSarray values in Xcode. I need to get array indexOfObject by comparing string values.
my array values are
(
{
firstName = lord;
lastname = krishna;
},
{
firstName = priya;
lastname = amirtha;
}
)
If I type first name in textfield and click button means last name want to display in another textfield.
thank you.
To answer the title of your question:
NSString *compareString = #"something";
NSMutableArray *indexesOfMatches = [[NSMutableArray alloc]init];
for (NSString *string in theArray) {
if ([string isEqualToString:compareString]) {
NSNumber *index = [[NSNumber numberWithInterger:[theArray indexOfObject:string]];
[indexOfMatches addObject:index];
}
}
//indexOfMatches will now contain NSNumber objects that represent the indexes of each of the matching string objects in the array
I think that using an NSDictionary would be better for you though. Then you can simply keep the first and last names as Key Value pairs.
NSDictionary *names = #{#"lord" : #"krishna", #"priya" : #"amirtha" };
Then you can just do value for key when you get the first name:
NSString *firstName = #"lord";
NSString *lastName = [names valueForKey:firstName];
Store firstNameArray and lastNameArray a mutable array NSMutableArray.
Using Fast Enumeration. Suppose array is the array you are provided with
for (NSDictionary *item in array) {
[firstNameArray addObject:[item objectForKey:#"firstName"]];
[lastNameArray addObject:[item objectForKey:#"lastName"]];
}
After entering the data in firstNameTextField click the button
Button action method implementation
-(IBAction)btnClicked:(id)sender {
NSInteger index = [firstName indexOfObject:[firstNameTextField text]];
[lastNameTextField setText:[lastName objectAtIndex:index]];
}

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]];

Xcode NSDictionary Subdictionary

I have dictionary which i want to use to fill a tableview. It is parsed by JSON.
My dictionary looks like that:
NSLog(#"%#",temp);
// OUTPUT //
(
{
ShootingDate = "2013-07-29 00:00:00";
ShootingID = 1;
ShootingName = Testshooting;
},
{
ShootingDate = "2013-06-12 00:00:00";
ShootingID = 2;
ShootingName = Architektur;
}
)
Dictionary looks in XCode like that:
Now i want to fill a table with that data. Each row should display ShootingDate,ShootingID and ShootingName but i am not able to access these keys.
Anyone a suggestion?
First of all temp is not dictionary it is NSArray and u can get it as
for (NSDictionary *dictionary in temp) {
[dictionary valueForKey:#"ShootingDate"];
[dictionary valueForKey:#"ShootingID"];
[dictionary valueForKey:#"ShootingName"];
}
You can get your dictionary in cellforRow as
NSDictionary *tempDicts=[temp objectAtIndex:indexPath.row];
cell.textLabel.text=[NSString stringWithFormat:#"id=%#,date=%#,name=%#",[tempDicts valueForKey:#"ShootingID"],[tempDicts valueForKey:#"ShootingDate"],[tempDicts valueForKey:#"ShootingName"]];
you can access these keys as
NSString *str_ShootingDate = [[temp objectAtIndex:indexpath.row]
objectForKey:#"ShootingDate"]; //you can change this key with ShootingID
//or ShootingName

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