I created app that have tableView with a viewController, what it should do is after selecting an item move to the second viewController and open it with passed data. I am implementing the next thing in the RootTableView:
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selecteditem = [listOfItems objectAtIndex:indexPath.row];
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:[NSBundle mainBundle]];
dvController.selectedItem = selectedItem;
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}
this code should work basicly but after I am selecting an item its been selected(blue colored) and that it, its stays with the selected blue color and nothing happened...after i selecting another row the same thing happens, what's wrong with my code?
You simply need to disable the selection styling when adding cells to your table:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
just put following code into your code
[self.tableView deselectRowAtIndexPath:
[self.tableView indexPathForSelectedRow] animated:YES];
like i.e
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selecteditem = [listOfItems objectAtIndex:indexPath.row];
// implement my code
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:[NSBundle mainBundle]];
dvController.selectedItem = selectedItem;
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}
I think you should check the navigationController. It might be not initialized.
You must have a UINavigationController to push any controller inside.
Related
I've been trying to push a uiwebview from a tableview to no avail. This is the code I have right now and whenever I click the cell, it would crash and show me the error.
Courses.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([[array objectAtIndex:indexPath.row] isEqual:#"GolfClub"])
{
Blue *changi = [[Blue alloc] initWithNibName:#"Golf Club" bundle:nil];
[self.navigationController pushViewController:changi animated:YES];
[changi release];
}
else if ([[array objectAtIndex:indexPath.row] isEqual:#"ExecutiveGolfCourse"])
{
ExecutiveGolfCourse *egc = [[ExecutiveGolfCourse alloc] initWithNibName:#"Executive Golf Course" bundle:nil];
[self.navigationController pushViewController:egc animated:YES];
[egc release];
}
newsClicked.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:newsClicked animated:YES];
}
initWithNibName is not meant for the title of your ViewController
nib name is actually the name of the .xib file that is associated with that ViewController
I have a UITableViewController working just fine with all the content when I tap on one of the cells didSelectRowAtIndexPath is being call but doesn't go to the next UITableViewController. Here is my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewHeadersController *detailViewController = [[ViewHeadersController alloc] initWithNibName:#"ViewHeadersController" bundle:nil];
NSArray *resultsData = [_response objectForKey:#"results"];
detailViewController.urlHeaders = [[resultsData objectAtIndex:indexPath.row] objectForKey:#"url"];
[self.navigationController pushViewController:detailViewController animated:YES];
}
I know didSelectRowAtIndexPath is responding because I see the output of the NSLog.
Any of you knows what is going on or why the uitablevicontroller is not switching to the next UITableViewController?
I'll really appreciate your help
I change the line where is navegationController:
ViewHeadersController *detailViewController = [[ViewHeadersController alloc] initWithNibName:#"ViewHeadersController" bundle:nil];
detailViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
NSArray *resultsData = [_response objectForKey:#"results"];
detailViewController.urlHeaders = [[resultsData objectAtIndex:indexPath.row] objectForKey:#"url"];
[self presentViewController:detailViewController animated:YES completion:nil];
i am new to iphone ,my question is in one view i placed a tableview and display some data on that view,i want to add another view(same view to all cell) when select cell(suppose select first cell view will display and newly add view is used to delet that cell information based on cell index)please tell me how to display view.
In the didSelectRowAtIndexPath method in your TableView class; that is where you deal with the selected row. Here is some code that pushes a webViewController onto the screen :
webViewController = [[WebViewController alloc] init];
WebViewController *wvc = [[WebViewController alloc] init];
NSMutableArray *temp = [eventsDict objectForKey:[datesArray objectAtIndex:[indexPath section]]];
EventObject *tempEvent = [temp objectAtIndex:[indexPath row]];
NSString *eventURL = [tempEvent eventLink];
[wvc setEventWebsite:eventURL];
[self setWebViewController:wvc];
[[self navigationController] pushViewController:webViewController animated:YES];
[wvc release];
[webViewController release];
Basically I have an array of Events, it gets the event you touched using [indexPath row], gets the URL within that Event object, passes it to the webViewController, then it pushes the webViewController onto the screen loading the appropriate link. Hope this helps
If you want to display the same view for all cells - simply create one UIView object and change frame in didSelectRowAtIndexPath delegate method. It will something like this -
At first declare infoView in your .h file UIView *infoView, next steps:
- (void)viewDidLoad {
infoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
infoView.hidden = YES;
[tableView addSubview:infoView];
[super viewDidLoad]; }
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CGPoint st = CGPointMake(tableView.frame.size.width/2, cell.frame.size.height*indexPath.row);
infoView.hidden = NO;
infoView.center = st;}
Don't forget to release infoView in dealloc method
Hello I am trying to push different nibfiles into view
When you click On the uitablecell it corresponds to.
I have no Idea where to start.
Read UITableView class and navigation based app and see this code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//and code like this
if(indexPath.row==0)
{
FirstController *objFirstController=[[[FirstController alloc] initWithNibName:#"FirstView" bundle:nil] autorelease];
[self.navigationController pushViewController:objFirstController animated:YES];
}
else
{
SecondController *objSecondController=[[[SecondController alloc] initWithNibName:#"SecondView" bundle:nil] autorelease];
[self.navigationController pushViewController:objSecondController animated:YES];
}
}
How do make the individual cells go to different View Controller. Right now, using the method im using, if i click the first cell, it takes me to a view controller where i have a pic. now if i go back and click the second cell, it takes me to the same view controller. How do i change this so that all the other individual cells go to individual view controller? what do i add or what do i configure?
here my code:
// Customize the appearance of table view cells.
- (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];
}
// Configure the cell...
NSUInteger row = [indexPath row];
cell.textLabel.text = [breakdownArray objectAtIndex:row];
bookDetailViewController.title = [NSString stringWithFormat:#"%#" , [breakdownArray objectAtIndex:row]];
return cell;
}
- (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];
*/
NSInteger row = [indexPath row];
if (self.bookDetailViewController == nil); {
BookDetailViewController *aBookDetail = [[BookDetailViewController alloc] initWithNibName:#"BookDetailView" bundle:nil];
self.bookDetailViewController = aBookDetail;
[aBookDetail release];
}
bookDetailViewController.title = [NSString stringWithFormat:#"%#" , [breakdownArray objectAtIndex:row]];
Books2010AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.breakdownNavController pushViewController:bookDetailViewController animated:YES];
}
One fairly easy way is in your tableView:didSelectRowAtIndexPath: method. You can check the index path parameter and execute different code paths. Something like:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath {
switch(indexPath.row) {
case 1:
[self.navigationController pushViewController:someViewController animated:YES];
break;
case 2:
[self.navigationController pushViewController:someOtherViewController animated:YES];
break;
}
}
Like most design choices, this is just an example of one possible approach. That's the best I can do in this general case.
I would create a new view controller each time instead of using the same one and just changing its properties, more like the example that you have commented out in tableView:didSelectRowAtIndexPath:. This will only work if you don't access your controller from anywhere other than these methods.
First: Remove bookDetailViewController.title = [NSString stringWithFormat:#"%#" , [breakdownArray objectAtIndex:row]]; from your tableView:cellForRowAtIndexPath: method (you should do this anyway, since it is just taking more processor time)
Second: Remove the bookDetailController property from your class. You won't need it anymore.
Third: Implement tableView:didSelectRowAtIndexPath: as follows:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path {
BookDetailViewController *bookDetailViewController = [[BookDetailViewController alloc] initWithNibName:#"BookDetailView" bundle:nil];
NSInteger row = path.row;
bookDetailViewController.title = [NSString stringWithFormat:#"%#" , [breakdownArray objectAtIndex:row]];
//Any other setup on view controller
Books2010AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.breakdownNavController pushViewController:bookDetailViewController animated:YES];
[bookDetailViewController release];
}