Getting an ID string when selecting a cell - iphone

I am making an iPhone app with different views, one of these being an UITableView, and i want to pass an ID string to another view, depending on the selected row.I have multiple row selection.I don't want to pass the data of the cell, but an ID to associate with it.
F.E: Cell name: "United States", to return a NSString: "09r454-0567-34".I don't have an idea of how to associate these strings to a cell.Thanks in advance.

Maybe you need to declare an array of dictionaries, then you catch didSelectRowAtIndexPath:, and retreive the information in that array
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dict = [myIDArray objectAtIndex:indexPath.row];
NSLog(#"ID %#", [dict objectForKey:#"ID"]);
NSLog(#"Name %#", [dict objectForKey:#"Name"]);
}
NSArray* myIDArray; //must be declared in .h
Hope this help...

Related

How to display json data in UITableviewcell in iphone [duplicate]

This question already has an answer here:
How to display Json data in UITableviewCell
(1 answer)
Closed 9 years ago.
I am currently working in JSON. I am just using a JSP webserver. Using this server, I want to display ticket number, time and status, which is to be displayed in the table view. I dont know how to display the JSON DATA in UITableview.Please give me idea any body. I am new to the ios progrmming.Thanks in advance.
I am menctioned below is my Json data.
{"result":[{"request_id":587,"ticket_number":"P_1000587","email":"hars","user_id":6,"description":"","createdTime":"10/15/2013
06:15:06
PM","status":"initiated"},{"request_id":586,"ticket_number":"P_1000586","email":"h14s","user_id":6,"description":"fdyfyt","createdTime":"10/15/2013
06:12:56
PM","status":"initiated"},{"request_id":585,"ticket_number":"P_1000585","email":"har","user_id":6,"description":"","createdTime":"10/15/2013
06:12:29 PM","status":"initiated"},
To get NSDictonary out of JSON use:
NSError* error = nil;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
To create a UITableView read tutorials on Apple:
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/CreateConfigureTableView/CreateConfigureTableView.html
Step 1 Declare Dictionary in .h file
NSDictionary* dictJson;
Step 2 Parse JSON Data and store it into Dictionary
NSError* error = nil;
dictJson = [[NSDictionary alloc] initWithDictionary:[NSJSONSerialization JSONObjectWithData:YOURDATA options:kNilOptions error:&error]];
Step 3 User following TableView Methods to fill your data and display it
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if(!cell)
cell = [[UITableViewCell alloc] init];
// Display Ticket_number in Text Field of TableView Cell
[cell.textLabel setText:[[[dictJson objectForKey:#"result"] objectAtIndex:indexPath.row] valueForKey:#"ticket_number"]];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return Number of Objects in Result
return [[dictJson objectForKey:#"result"] count];
}

Alternate way to get object based on indexPath in UITableView

I was following a tutorial: http://www.devx.com/wireless/Article/43374
so I could add alphabet sorting and panning to my UITableView of songs and I have finished coding but this method I have followed slows down the UITableView by filtering arrays and retrieving values in the cellForRowAtIndexPath method.
I cant figure out how I can remove the excess coding to increase the speed. All the MPMediaItems are stored in the tableTracks array. Which is initialized in viewDidLoad. And thee musicIndex is an array of the alphabets(first letter of each song). I extended MPMediaItem to include an NSString firstLetter that is the first letter of the song.
Any help speeding it up?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//--Create Cell--\\
.........
//--Load Info--\\
NSString *alphabet = [musicIndex objectAtIndex:[indexPath section]];
NSPredicate *predicate =
[NSPredicate predicateWithFormat:#"firstLetter beginswith[c] %#", alphabet];
NSArray *songs = [tableTracks filteredArrayUsingPredicate:predicate];
//Needed Object
MPMediaItem *item = [songs objectAtIndex:indexPath.row];
//--Rest of Method--\\
...........
return cell;
}
If you are showing separate sections, one for each letter of the alphabet, I would create an array of dictionaries from my data, in viewDidLoad, not here. The dictionaries would have the first letter of the song as the key, and an array of songs as the value. That way, all the filtering and sorting is done up front, rather than in each row as the table is populated.

I want to populate my tableview from my NSDictionary

- (void)receiveData:(NSData *)data {
self.office = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
self.files = [office objectForKey:#"files"];
[self.myTableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.files.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] init];
NSDictionary *file = [self.files objectAtIndex:indexPath.row];
cell.textLabel.text = [file objectAtIndex:1]; //here i want to get data from the array
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
cell.textLabel.numberOfLines = 1;
return cell;
NSLog(#"files:\n%#", file);
}
I have a json response that i have stored in NSDictionary offices, and array in that json is stored in NSArray files,now i have given files array to another NSDictionary file and according to data that stored in that file i want that data in my table row, but i don't have any "key" in my files array only value in that array,
so my question is that how can i point that value in that my file dictionary, i have make comment where i want that please see that comment....
You're creating a dictionary:
NSDictionary *file = [self.files objectAtIndex:indexPath.row];
NSDictionary is indexed with keys, so you'll have to obtain the string with objectForKey:
cell.textLabel.text = [file objectForKey:#"fileName"];
I think you must either misunderstand how an NSDictionary works or else the objects that you're accessing in self.files are not actually NSDictionaries. If they are in fact NSDictionary objects they need to be accessed like runmad says with
[file objectForKey:#"keyName"]
If there are no keys then you are dealing with a different type of object and we would need to know more specifics to be able to help you. As you posted the question runmad has given you the correct answer.
The JSON parser NSJSONSerialization may not be creating the NSDictionary/NSArray structures you think you have. You'll have to check your JSON, e.g. with jsonlint.com, then use NSLog to have a look at what is placed in your dictionaries/arrays. Also try some of the NSJSONReadingOptions options.

Populating Two UITableView in the same View

I have 2 UITableView in the same View and my question is how to populate them with different data in the viewDidLoad method? thanks.
To answer the plist part of your question:
NSString *filePath=[[[NSBundle mainBundle] pathForResource:#"someName" ofType:#"plist"] retain];
NSDictionary *dict = [[NSDictionary dictionaryWithContentsOfFile:filePath] retain];
So just use two of these, one for each table, then just have two tableView, as it appears you already understand.
You populate them in the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
and in that function you can compare the tableView to see what table it is, and load the appropriate data for that table.
//Assume the following property
#property (nonatomic, retain) NSDictionary *myData;
add this to the Code file
- (NSDictionary) myData
{
if (myData == nil) //Notice I am accessing the variable, not the property accessor.
{
NSString *filePath=[[NSBundle mainBundle] pathForResource:#"someName" ofType:#"plist"];
myData = [[NSDictionary dictionaryWithContentsOfFile:filePath] retain];
}
return myData;
}
then when you access the self.myData property it will open up if it is not opened already.
All delegate and datasource protocol methods always pass a pointer to the tablesView they are called for. Just do an if to check which is meant in each method.

UITAbleView not in alphabetical order?iPhone

I have a tableView with several Sections being populated from a plist of NSDictionaries
How do I have it arrange the sections in the order they are in in the NSDictionary instead of alphabetically?
NSDictionary is unordered. You should use an NSArray (or std::vector, or std::map, etc.) as the data source.
To get the keys, use -allKeys. To get a sorted array from it, use -sortedArrayUsingSelector:.
I improvised a solution. I added a "01", "02", "03" etc to the beginning of each dictionary name then just added the code to remove those two characters before displaying it:
-(NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section{
NSString *key = [keys objectAtIndex:section];
key = [key substringFromIndex:2];
return key;
}