uitableView didSelectRowAtIndexPath "selected row not loading the detail view" - iphone

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

Related

Passing Segue Gives Error

Hi I have currently made a push on select to get my table view to push to a detail view. I also have this code to send the name of the table cell that was selected:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *acell = [tableView cellForRowAtIndexPath:indexPath];
selectedCell = acell.textLabel.text;
DetailViewController *myDetViewCont = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
myDetViewCont.navigationItem.title = selectedCell;
[self.navigationController pushViewController:myDetViewCont animated:YES];
}
It builds successfully but in the simulator It throws an error:
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
Thread: 1 signal: SIGABRT
I've looked up similar problems but havent found a solution.
Any Help?
Ok additional info on the error:
[self.navigationController pushViewController:myDetViewCont animated:YES];
If you're using storyboards, then the following line doesn't make sense:
DetailViewController *myDetViewCont = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
Do you have a NIB file called DetailViewController.xib in your project? Presumably not. Thus myDetViewCont will be nil and you'll get an exception. If you do have that NIB, then what is the push segue going between (because you can't segue from a storyboard to a NIB)?
Assuming that you really want to use storyboards, and not NIBs, if you already have a push segue between the controllers, give that segue an identifier (you do this in Interface Builder; in my code below I'll just use youridentifier as a placeholder for whatever you specify, which you'll replace with the correct identifier) and then you should transition using the segue:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *acell = [tableView cellForRowAtIndexPath:indexPath];
[self performSegueWithIdentifier:#"youridentifier" sender:acell];
}
If you want to send data to the new controller, you'd use prepareForSegue to do that:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"youridentifier"])
{
if ([sender isKindOfClass:[UITableViewCell class]])
{
UITableViewCell *selectedCell = sender;
UIViewController *myDetViewCont = segue.destinationViewController;
myDetViewCont.navigationItem.title = selectedCell.textLabel.text;
}
else
{
NSLog("%s Was expecting sender to be a tableviewcell", __FUNCTION__);
}
}
}
Found the solution to this. You need to have a UINavigationController in your story board. To do this take your first scene, click on it and go to tabs at top of xcode and do Editor>Embed in>Navigation controller, check the button to set the navigation controller as inital scene. Add the following code to hide the navbar at top.
self.navigationController.navigationBar.hidden = YES;
self.navigationController.navigationBar.translucent = NO;

Select TableRow and SelectionColor should be visible and then go off automatically

What I was trying - I have a UITableView. On selecting any particular row it should show a selectionColor(Blue etc) and then it should disappear. Any property/code to do this? I am actually applying in in MyCalender view.
You can use [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
in your - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
method.
You should refer the delegates and datasource in tableview for this
The datasource sets the inputs to table
The delegate gives you callbacks of actions on the tableview .
So write the delegate methods ,include it in classdef UITableViewDelegate,connect it in xib
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
This delegate method is invoked if you click on a particular row.The particular row is the row at the "indexpath"
finally include this line which deselect the code
[tableView deselectRowAtIndexPath:indexPath animated:YES];
add this to your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
`
UIView *violetColor = [[[UIView alloc] init] autorelease];
//violetColor.backgroundColor = [UIColor colorWithRed:0.824 green:0.749 blue:0.553 alpha:0.70];
violetColor.backgroundColor = [UIColor colorWithRed:0.724 green:0.749 blue:0.953 alpha:0.70];
cell.selectedBackgroundView = violetColor;
`
and then add this to your didselect `
if (indexPath != nil) {
[mTableView deselectRowAtIndexPath:indexPath animated:YES];
}
`.i hope this will do the trick.
In delegate method didSelectRowAtndexPath use this code
[tableView deselectRowAtIndexPath:indexPath animated:YES];
If you are using UITableView and want to remove selection of the UITableViewCell. Then you have to use the below code to deselect tableview cell.
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];

dealloc of view controller is not called?

i dont know why the dealloc of the viewcontroller is not calling
please see the code snippet.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
SaleItemsVC *itemsObj = [[SaleItemsVC alloc] initWithNibName:#"SaleItemsVC" bundle:nil];
[self.navigationController pushViewController:itemsObj animated:YES];
EventSingleEntity *entityobj=(EventSingleEntity*)[arrSales objectAtIndex:indexPath.row];
itemsObj.eveintEntityRef=entityobj;
[itemsObj loadProductsOfEventId:entityobj.event_id];
itemsObj.EventTitle.text=entityobj.name;
itemsObj.EventEndDate.text=entityobj.end;
SalesCell *cell=(SalesCell*)[tableView cellForRowAtIndexPath:indexPath];
itemsObj.eventImage=cell.imgCenter.image;
[itemsObj release];
}
You create itemsObj, the controller, so the ref count == 1
You push itemsObj to self.navigationController, refcount == 2
You release itemsObj ref count == 1
It should not be released until self.navigationController releases it.

iPhone - Load a new UIView on pressing button

say I have a UITableView and upon selecting a new table row (thus firing the delegate method), I want to load a new UIView (which I have created in storyboard) and pass to it a data item.
Obviously the new UIView is added as a UIViewController in storyboard and has its own controller class..
How would I go about doing that?
Thank you!
Try something along the lines of this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:dvController animated:YES];
}
If you are using storyboards and not XIB files, use this code...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *dvController = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
[self.navigationController pushViewController:dvController animated:YES];
}
Also you need to set the "Identifier" field in Interface Builder to the name used in self.storyboard instantiateViewControllerWithIdentifier:
Cheers!

Call UIViewController from "subview"

I have a UIViewController which when it loads it loads up this..
MapViewController *mapController = [[MapViewController alloc] initWithNibName:#"MapView" bundle:nil];
self.mapViewController = mapController;
[self.view insertSubview:mapController.view atIndex:0];
[mapController release];
I also have a switch views button that can change to a table view....
if (self.tableViewController ==nil)
{
TableViewController *tableController = [[TableViewController alloc] initWithNibName:#"TableView" bundle:nil];
self.tableViewController = tableController;
[tableController release];
//[self.view insertSubview:detailController atIndex:0];
}
if (self.mapViewController.view.superview == nil)
{
[tableViewController.view removeFromSuperview];
[self.view insertSubview:mapViewController.view atIndex:0];
}
else
{
[mapViewController.view removeFromSuperview];
[self.view insertSubview:tableViewController.view atIndex:0];
}
I am trying to change the view to a detail view based on selecting a row in the table view and I cannot work out how to call it at all. All methods I have seem to fail! Please help!
Add the UITableViewDelegate protocol to your controller class.
#interface myTableViewController : UITableViewController <UITableViewDelegate>
When you create your table veiw controller set its delegate to be your controller using:
myTableViewController.delegate = self; // Assuming your setup code runs within the table view controller
In your tableViewController, implement didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int rowSelected = [indexPath indexAtPosition:0]; // Assuming your UITableView has a single section.
The class you should look at for handling row selection is UITableViewDelegate, which has the method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath