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/
Related
I have a table view with 7 rows, I want to click on my rows and load the detailView( uiViewController )
I can select the row and I can see the Log in console but it never loads the detail view
would you please give me some hits, what is the problem?
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(#"selected rows");
MyBookDetailView *c = [[MyBookDetailView alloc] init];
[self.navigationController popToRootViewControllerAnimated:NO];
[self.navigationController pushViewController:c animated:YES];
}
I also try performSelectorOnMainThread but still it just clickable and I have problem to load my view controller, I also add delegate in - (void)viewDidLoad method,
Thanks in advance!
lates code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
MyBookDetailView *c = [[MyBookDetailView alloc] initWithNibName:#"MyBookDetailView" bundle:nil];
[self.navigationController pushViewController:c animated:YES];
}
Why you want to pop up all controllers withing navigationcontroller stack to root controller and same time your want to push up new MyBookDetailView (i hope its base class is UIViewController).
Anyways, MyBookDetailView *c = [[MyBookDetailView alloc] init]; also will not work for you. Because c object of UIViewController (MyBookDetailView) view is nil. I recommend use break point trace execution stack and variable that you trying to remove and adding on run time you will better know what going on in your program.
I think following code may work for you,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
MyBookDetailView *c = [[MyBookDetailView alloc] initWithNibName:#"WriteYourNibNameIfYouCreate" bundle:nil];
[self.navigationController pushViewController:c animated:YES];
}
While using the DetailViewController the value is not passing from the UITabelViewController, is it any wrong with the code?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
detailedvu *dvController = [[detailedvu alloc] initWithNibName:#"detailedvu" bundle:[NSBundle mainBundle]];
if (0 == indexPath.row)
{
NSLog(#"0");
dvController.lb1.text = #"Make";
}
[self.navigationController pushViewController:dvController animated:YES];
}
Please help me to sort out.
The dvcontroller is instantiated correctly.The problem here is that the lbl is a label from the nib(i think).So when pushed to the viewcontroller a new instance is created so the setting here does not make any sense.
Pass it in a data holder such as NSString and load the label in the viewdidLoad of the detailedvu
One suggestion : Please try to follow the naming conventions for better readability
Happy Coding :)
You can achive this using this.
Create a string variable property in detailedvu.h file :
#property (strong, nonatomic) NSString * str;
And in detailedvu.m file add this :
#synthesize str;
- (void)viewDidLoad
{
[super viewDidLoad];
if (str.length!=0) {
NSLog(#"hi%#",str);
lb1.text=str;
}
}
And in your table vc .m file put this code :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
detailedvu *dvController = [[detailedvu alloc] initWithNibName:#"detailedvu" bundle:[NSBundle mainBundle]];
if (0 == indexPath.row)
{
NSLog(#"0");
dvController.str = #"Make";
}
[self.navigationController pushViewController:dvController animated:YES];
}
My first post here. Seems I have run into a bit of a memory leak problem.
I have a tab bar with two columns, one for salad and one for sushi. In each tab I also have a tableview that displays the different food choices. When the user presses one of the cells the app takes the user to a more detailview that displays a larger image and some info about the chosen food menu.
But when I run leaks I get a 98,5% leak on the following line.
[self.navigationController pushViewController: detail animated: YES];
if I release detail after this line, the app crashes.
Here is the rest of the function code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *message = nil;
NSMutableString *image_string = nil;
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:#"detailview"];
if(tableView == SushiTable)
{
message = [sushiNames objectAtIndex:indexPath.row];
image_string = [NSMutableString stringWithString:[sushiImages objectAtIndex:indexPath.row]];
}
if(tableView == SaladTable)
{
message = [saladNames objectAtIndex:indexPath.row];
image_string = [NSMutableString stringWithString:[saladImages objectAtIndex:indexPath.row]];
}
[image_string deleteCharactersInRange: [image_string rangeOfString: #"_small"]];
NSMutableString *temp_str = [[message copy] autorelease];
NSString *final = [[[temp_str stringByReplacingOccurrencesOfString:#" " withString:#"_"]
stringByReplacingOccurrencesOfString:#"รค" withString:#"a"]
lowercaseString];
detail.food_name = message;
detail.image_name = image_string;
detail.food_info_key = final;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.navigationController pushViewController: detail animated: YES];
detail = nil;
}
EDIT:
I saw also that in my viewDidLoad function in DetailViewController the following line has a memory leak, don't know if it's maybe related.
food_image.image = [UIImage imageNamed:image_name];
EDIT2:
Also I'm running on the simulator.
Try it.
[self.navigationController pushViewController: detail animated: YES];
[detail release];
detail = nil;
When you push an object.Now it is added into navigation stack array, Now array owner of this object
so you should need release object which you are added in an array.
Declare the detail globally and release it in dealloc:. It may work.
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.
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...