Use dictionary with didSelectRowAtIndexPath - iphone

I am trying to get the trail name from the selected cell and pass it on to the next view in didSelectRowAtIndexPath. How would I go about this?
http://pastebin.com/bgXNfjie

if I understood you correctly, then this is not such a big thing. You just have to make a property in TrailViewController to hold your value and assign it like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
TrailViewController *trailViewController = [[TrailViewController alloc] initWithNibName:#"TrailViewController" bundle:[NSBundle mainBundle]];
NSDictionary *dict = [rows objectAtIndex: indexPath.row];
trailViewController.trailName = [dict objectForKey:#"name"];
[self.navigationController pushViewController:trailViewController animated:YES];
[trailViewController release];
}
Instead of just the name, you'll probably want to assign the complete NSDictionary to a property of the TrailViewController,but thats up to you. I hope I could help...

Related

Change data source of tableView after selected row

I have table view with rows...after I selecting one item I want to show tableView with a new data source.
I tried to implement:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.searchDisplayController setActive:YES animated:YES];
Game *game = [self.filteredListContent objectAtIndex:indexPath.row];
//name your class names with capitals
NSMutableArray *arrayToBeAdded= [[NSMutableArray alloc]initWithArray:newgamearray];
[ListContent removeAllObjects];
[ListContent addObject:arrayToBeAdded];
but I getting exeption in the
[ListContent removeAllObjects];
Implementation of the init:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellID = #"cellSgames";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
Gmage *game = [self.ListContent objectAtIndex:indexPath.row];
In the .h file i declare:
NSArray AllList;
NSMutableArray ListContent;
any ideas?
Instead of manipulating the current list, create new UITableViewController with your desired data source. If you are using the UINavigationController it will create a better user experience and you will avoid your current problem.
Example:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
InnerTableViewController *innerTVC = [[InnerTableViewController alloc] initWithNibName:#"InnerTableViewController" bundle:nil];
[self.navigationController pushViewController:innerTVC animated:YES];
[innerTVC release];
}
Normally if you are resetting the array in didselect method and reloading your table view with new content it should work.
In your case i think at line:
[ListContent removeAllObjects];
ListContent is not a valid pointer. I don't see a reason to crash it here.

Load a DetailViewController in TableViewcontroller

I just followed this tutorial:
Implementing UITableView Sections from an NSArray of NSDictionary Objects
This is a good example. But I want to add more fonctions. I want to load a detailView. I followed more than 5 tutorials to do it, but I fail every time. After 4 hours, i'm asking you.
How can I load a detailView with this sample?
Regards, Fanny
Already tried, nowi have this code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedRow = nil;
NSDictionary *dictionary = [self.books objectAtIndex:indexPath.row];
selectedRow = [dictionary objectForKey:#"Title"];
//Initialize the detail view controller and display it.
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:[NSBundle mainBundle]];
dvController.selectedRow = selectedRow;
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
dvController = nil;
But nothing appen :/
Hope this tutorial help you.
http://www.xcode-tutorials.com/uitableview-%E2%80%93-2-loading-a-detail-view/

Select multiple items from a tableview - Beginner

I need to add a TableView and i should be able to click several Items in that tableview and save it to a NSMutable dictionary or something suitable.
I know that you have to use a NSMutable Dictionary for this. But i don't understand how to do this.
Can someone please point a good tutorial or provide some sample codes for me.
You will have to use a delegate method for that.
First make sure your table view is set up well (delegate and datasource) and then
implement delegate 's :
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath.
You access the selected row index with : [indexPath row].
And then you can store this value.
THere was a post by Matt Gallagher at Cocoa With Love about this that you might find illuminating.
A bit dated, but the principles will be the same.
It really depends on your data model.
First of all you need a NSMutableArray not a NSMutableDictionary;
//declare an NSMutableArray in the interface
#interface Class : SuperClass {
NSMutableArray *_arrayWithMySelectedItems;
}
//in one of your init/preparation methods alloc and initialize your mutable array;
- (void)viewDidLoad {
[super viewDidLoad];
_arrayWithMySelectedItems = [NSMutableArray alloc] init];
}
//now before you forget it add release in your dealloc method
- (void)dealloc {
[_arrayWithMySelectedItems release];
[super dealloc];
}
//add this following code to your didSelect Method part of tableView's delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//if you have only one category, you will probably have something like this
[_arrayWithMySelectItems addObject:[dataModelArray objectAtIndex:indexPath.row];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
You can use NSMutableDictionary in this method as:
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *dict = [NSMutableDictionary alloc] init];
[dict setObject:[NSString stringWithFormat:"%#", cell.Label.text] forKey:[NSString stringWithFormat:"Key%d", indexPath.row]];
}
declare a string inside didselect row of tableview then give that string = [your populated array objectAtIndex:indexpath.row]; then add that into dictionary or nsmutable array according to your wish.

UITableView with JSON help

I am trying to feed in some JSON data to my iPhone app, the data is coming in fine as I have NSLog's telling me so.
The problem I am having is trying to get the results to show in a UITableView. I have a navigation controller underneath a tab bar controller, the navigation controller contains a table view controller which loads another NIB file with a table view connected to a class which is the delegate and data source delegate.
I also need to categorize the results into sections - these being
England
Scotland
Wales
N.Ireland
To get an idea of what JSON string I am using see this one.
As you can see the JSON does not cater for the sections but I am yet to implement this, so i would need to know beforehand so I do not have to amend much code later on.
OK - I am using Stig JSON parser.
Here is my ListVenuesView.h (connected to table view)
#import <UIKit/UIKit.h>
#import "SBJson.h"
#interface ListVenuesView : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
IBOutlet UITableView *venueList;
NSMutableDictionary *jsonArray;
}
#property (nonatomic, retain) IBOutlet UITableView *venueList;
#property (nonatomic, retain) NSMutableDictionary *jsonArray;
#end
jsonArray is used to store the JSON data and eventually the proper array.
And here is my ListVenuesView.m (key areas in question)
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"Table View Loaded");
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
// This is where we load the JSON data
NSURL *jsonURL = [NSURL URLWithString:#"http://www.thebigfishexperience.org.uk/sources/ajax/venue-json.php?location=peterborough"];
NSString *jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL];
NSLog(#"%#", jsonData);
// Convert jsonData to array
self.jsonArray = [jsonData JSONValue];
NSLog(#"%#", jsonArray);
NSLog(#"count is: %i", [self.jsonArray count]);
// Release NSString and NSURL
[jsonURL release];
[jsonData release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.jsonArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSMutableDictionary *dict = [self.jsonArray objectAtIndex: indexPath.row];
cell.textLabel.font = [UIFont fontWithName:#"Arial" size:15.0];
cell.textLabel.text = [dict objectForKey:#"venueName"];
cell.detailTextLabel.text = [dict objectForKey:#"venueCordDist"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Configure the cell...
return cell;
Also how can I use the data in the cells to go to another subview of the nav controller which gives me a back button and displays the info from the JSON string just for that particular cell that has been tapped.
I think this has something to do with it? Not sure though as this is my first app i am building! So probably expect more pleas of assistance - ha ! ;)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"Nib name" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
}
On selecting a row, as mentioned by u, we are navigating to another view. Let us assume that the view controller is DetailViewController which is a sub-class of UIViewController.
In the DetailViewController.h , declare a NSDictionary object.
In DetailViewController.m, add
-(void)setVenueDict:(NSDictionary*)venueDict
{
if( _venueDict )
{
[_venueDict release];
}
_venueDict = [venueDict retain];
}
In ParentViewController, ur didSelectRow.. method should be like this.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"Nib name" bundle:nil];
// ...
// Pass the selected object to the new view controller.
NSDictionary *dict = [self.jsonArray objectAtIndex: indexPath.row];
[detailViewController setVenueDict:dict];
detailViewController.title = [dict objectForKey:#"venueName"];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
In the second view controller, u can do whatever u want with the _venueDict.
Nathan,
since you want to reuse the data parsed from the JSON feed over more than one ViewController the best way to approach this is to build an object Model so that you can pass the object for the selected row in the list to the detail ViewController.
I would also separate the JSON parsing code into a separate class and not keep it in the ViewController.
You can find classes to fetch JSON on this link.
The result from the custom code to parse the JSON feed would give back a NSDictionary with as keys the section names you mention. And the value in the NSDictionary for those keys would be an array of your custom objects that contain all the relevant data for one row (and detail screen).
Hope this helps you on your way.
jsonArray is NSMutableDictionary.
have to use
[jsonArray objectForKey:key];
//check this line
NSMutableDictionary *dict = [self.jsonArray objectAtIndex: indexPath.row];
this may help.

How do I set a detail view's title from the selected row?

I have a table view made up of an NSArray of instances of a custom object. When a row in the table view is tapped, it is supposed to trigger a detail view made up of a web view.
So, in MainViewController.m, I have the following:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MObjectDetailVC *mObjectDetailViewController = [[MObjectDetailVC alloc] initWithNibName:#"MObjectDetailVC" bundle:nil];
mObjectDetailViewController.detailURL=[[[mcData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] object.url]];
mObjectDetailViewController.title=[[[mcData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] object.name]];
[self.navigationController pushViewController:mObjectDetailViewController animated:YES];
[mObjectDetailViewController release];
}
However, I get an error for both that start with mObjectDetailViewController: Expected ']' before '.' token. and I don't know why. Can you help? Thanks!
UPDATE: I reduced the amount of square brackets, but I still have the error on each of those lines. It's just that instead of 3 of that error on each line, there's just one instance for each.
You have extra square brackets. Try this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MObjectDetailVC *mObjectDetailViewController = [[MObjectDetailVC alloc] initWithNibName:#"MObjectDetailVC" bundle:nil];
mObjectDetailViewController.detailURL=[[[mcData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] object.url];
mObjectDetailViewController.title=[[[mcData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] object.name];
[self.navigationController pushViewController:mObjectDetailViewController animated:YES];
[mObjectDetailViewController release];
}
At the end of the lines that were giving me issue, I called object.url and object.name. This was unnecessary and invalid. I just needed to put url and name to get those variables from my selected object.
Thanks for your help!