Trying to push to a new view from the results of a search and for some reason the code isn't being called. Sorry I can't be more descriptive.
Here's the code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.table deselectRowAtIndexPath:indexPath animated:YES];
int selectedTemplateID = [((CollectionItem*)[cllct objectAtIndex:indexPath.row]).tID intValue];
ACollection *newView = [[ACollection alloc] initWithID:selectedTemplateID];
newView.template = [cllct objectAtIndex:indexPath.row];
if(tableView == self.table) {
[self.navigationController pushViewController:newView animated:YES];
newView.theTitle = [self.table cellForRowAtIndexPath:indexPath].textLabel.text;
}
if(tableView == self.searchDisplayController.searchResultsTableView) {
NSLog(#"Should push");
[self.navigationController pushViewController:newView animated:YES];
newView.theTitle = [self.table cellForRowAtIndexPath:indexPath].textLabel.text;
}
}
Thanks
First check the datasource and delegat of tableview.
remove [self.table deselectRowAtIndexPath:indexPath animated:YES]; and check once.
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
In my Application i was using the different UIButtons as menue to Go Next class ,here is my Code
-(IBAction)GoToALevel
{
ALevel *obj = [[ALevel alloc] initWithNibName:#"ALevel" bundle:nil];
[self.navigationController pushViewController:obj animated:YES];
[obj release];
}
-(IBAction)GoToTLevel
{
TLevel *obj = [[TLevel alloc] initWithNibName:#"TLevel" bundle:nil];
[self.navigationController pushViewController:obj animated:YES];
[obj release];
}
-(IBAction)GoToNLevel
{
NLevel *obj = [[NLevel alloc] initWithNibName:#"NLevel" bundle:nil];
[self.navigationController pushViewController:obj animated:YES];
[obj release];
}
-(IBAction)GoToFLevel
{
FLevel *obj = [[FLevel alloc] initWithNibName:#"FLevel" bundle:nil];
[self.navigationController pushViewController:obj animated:YES];
[obj release];
}
Now i put all these catogories in UITableview.But can any one Guide me that how can get the Alevel when i click on first section of uitableview and so on .
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
here is problem how to go next class
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
switch (indexPath.row) {
case 0:
[self GoToALevel];
break;
case 1:
[self GoToTLevel];
break;
case 2:
[self GoToNLevel];
break;
case 3:
[self GoToFLevel];
break;
default:
break;
}
}
hope this helps. happy coding :)
You will have to write this code in DidSelectRowAtIndexPath(). i.e
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath.
Here you can specify the action according to the method. Before this use the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
to specify the what happens at the index of each cell i.e. indexpath.row.
I have a navigation view, when I click, I just show the row number.
In the mainView:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (read == nil) {
readNotice *aController = [[readNotice alloc]initWithNibName:#"readNotice" bundle:nil];
self.read = aController;
}
[read updateRowNumber:indexPath.row];
[[self navigationController] pushViewController:read animated:YES];
and in my readNotice class:
-(void)updateRowNumber:(int)theindex {
rowNumber = theindex + 1;
message.text = [NSString stringWithFormat:#"row %i was clicked", rowNumber];
NSLog(#"view did load row = %d", rowNumber);
}
readNotice.xib only contents a Label.
First time I click it shows "Label", but it works the following tries.
I suppose I need to initiliaze something that I missed, but I cant find it.
Thank you in advance
I m not able to understand why you made read variable .... ? use this ..
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
readNotice *aController = [[readNotice alloc]initWithNibName:#"readNotice" bundle:nil];
[self.navigationController pushViewController:aController animated:YES];
[aController updateRowNumber:indexPath.row];
}
may this will help you...
Use -
if (self.read == nil) {
readNotice *aController = [[readNotice alloc]initWithNibName:#"readNotice" bundle:nil];
self.read = aController;
}
--------
---------
[[self navigationController] pushViewController:self.read animated:YES];
Try this,
in tableview:didSelectRowAtIndex method,
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (read == nil) {
readNotice *aController = [[readNotice alloc]initWithNibName:#"readNotice" bundle:nil];
self.read = aController;
}
read.index = indexPath.row;
[[self navigationController] pushViewController:read animated:YES];
create a instance variable in readNotice as
#property(assign) int index;
#synthesize index;
In readNotice viewDidLoad method, you just call updateRowNumber
- (void)viewDidLoad
{
[super viewDidLoad];
[self updateRowNumber:index];
}
It will display the number as you expected
I am having some issues with pulling up specific ViewControllers depending on which row is selected in my UITableView. I have a table, which contains 3 sections, with 4 rows in each section.
When a row is selected I have it set to call a new view for each row, so 12 views in total to be called depending on which row is selected. The issue is, that only the first 4 views are called, when I tap row number 5, it displays a new view, but it pulls up the first view.
I have set my UITableView up using the following code;
carbData = [[NSArray alloc] initWithObjects:#"What Are Carbs?",#"Why Do I Need Carbs?",#"Simple Vs. Complex",#"Fun Five Facts",nil];
proteinData = [[NSArray alloc] initWithObjects:#"What is Protein?",#"Why Do I Need Protein?",#"Complete Vs. Incomplete",#"Fun Five Facts",nil];
fatData = [[NSArray alloc] initWithObjects:#"What Are Fats?",#"Why Do I Need Fats?",#"Saturated Vs. Unsaturated",#"Fun Five Facts",nil];
[self setTitle:#"Nutrition Table"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.carbData count];
return [self.proteinData count];
return [self.fatData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SimpleTableIdentifier] autorelease];
}
if(indexPath.section == 0)
cell.text = [carbData objectAtIndex:indexPath.row];
if(indexPath.section == 1)
cell.text = [proteinData objectAtIndex:indexPath.row];
if (indexPath.section == 2)
cell.text = [fatData objectAtIndex:indexPath.row];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if(section == 0)
return #"Carbohydrates";
if (section ==1)
return #"Protein";
else if (section ==2)
return #"Fats";
}
I then use the following code to determine which view is to be called, when a certain row is selected;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([[carbData objectAtIndex:indexPath.row] isEqual:#"What Are Carbs?"]) {
CarbsView1 *controller = [[CarbsView1 alloc] initWithNibName:#"CarbsView1" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
if ([[carbData objectAtIndex:indexPath.row] isEqual:#"Why Do I Need Carbs?"]) {
CarbsView2 *controller = [[CarbsView2 alloc] initWithNibName:#"CarbsView2" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
if ([[carbData objectAtIndex:indexPath.row] isEqual:#"Simple Vs. Complex"]) {
CarbsView3 *controller = [[CarbsView3 alloc] initWithNibName:#"CarbsView3" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
if ([[carbData objectAtIndex:indexPath.row] isEqual:#"Fun Five Facts"]) {
CarbsView4 *controller = [[CarbsView4 alloc] initWithNibName:#"CarbsView4" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
if ([[proteinData objectAtIndex:indexPath.row] isEqual:#"What is Protein?"]) {
ProteinView1 *controller = [[CarbsView4 alloc] initWithNibName:#"ProteinView1" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
The app compiles and builds fine, everything works as i'd like it too, however when looking at the titles used, when I try and select "What is Protein?" is pulls up the view for "what are Carbs?" which is in the first if statement.
Any help would be massively appreciated
"What is Protein?" is in section 1, row 0. "What are Carbs?" is in section 0, row 0. Notice that your didSelectRowAtIndexPath method, you don't check the section number anywhere. So when you tap section 1, row 0, your first if statement returns true. I think you want something more like:
if (indexPath.section == 0) {
if(indexPath.row == 0) {
//present appropriate view controller
} else if (indexPath.row == 1) {
} else if (indexPath.row == 2) {
} ...
} else if (indexPath.section == 1) {
if (indexPath.row == 0) {
} else if (indexPath.row == 1) {
} ...
} ...
I have a UITableViewController that is presented with a list of choices. After the user taps one, I'd like to return to the previous view. The return seems too quick with the code I'm using though. I'd like to pause for 0.2 seconds or so to give the user time to see their selection become checked. Here's the code I'm using now:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger oldSelection = [[selectedCriteria objectAtIndex:criteriaSection] integerValue];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Since there's a "none" selection, we don't deselect if the user taps the one that's already selected
if ([indexPath row] != oldSelection + 1) {
NSIndexPath *selectionIndexPath = [NSIndexPath indexPathForRow:oldSelection+1 // Shift down for "None"
inSection:[indexPath section]];
UITableViewCell *checkedCell = [tableView cellForRowAtIndexPath:selectionIndexPath];
[checkedCell setAccessoryType:UITableViewCellAccessoryNone];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
[selectedCriteria replaceObjectAtIndex:criteriaSection
withObject:[NSNumber numberWithUnsignedInteger:[indexPath row]-1]];
}
[[self navigationController] popViewControllerAnimated:YES];
}
Is there a good way to add a short delay before the view controller is popped?
Hope this help.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger oldSelection = [[selectedCriteria objectAtIndex:criteriaSection] integerValue];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Since there's a "none" selection, we don't deselect if the user taps the one that's already selected
if ([indexPath row] != oldSelection + 1) {
NSIndexPath *selectionIndexPath = [NSIndexPath indexPathForRow:oldSelection+1 // Shift down for "None"
inSection:[indexPath section]];
UITableViewCell *checkedCell = [tableView cellForRowAtIndexPath:selectionIndexPath];
[checkedCell setAccessoryType:UITableViewCellAccessoryNone];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
[selectedCriteria replaceObjectAtIndex:criteriaSection
withObject:[NSNumber numberWithUnsignedInteger:[indexPath row]-1]];
}
[self performSelector:#selector(dismissController) withObject:nil afterDelay:0.2];
}
This give you a 0.2 seconds delay to call the function "dismissController".
The function "dismissController".
- (void) dismissController {
[[self navigationController] popViewControllerAnimated:YES];
}
Have you tried -performSelector:withObject:afterDelay ?
sleep(0.2) worked for me