navigation view doesn't work first click - iphone

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

Related

Push to new view from SearchDisplayController

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.

How to link a UITableViewController to multiple ViewControllers and how to call them

I'm trying to link cells from an UITableViewController to UIViewController, but each cell has to call a different UIViewController (so I imagine that I'll have to create one link to each view controller) and I don't know how to link them through storyboard.
This is what I have until now:
MainView.h
#interface MainView : UITableViewController <UITableViewDelegate, UITableViewDataSource>
{
NSArray *tableData;
}
#property (nonatomic, retain) NSArray *tableData;
#end
MainView.m
- (void)viewDidLoad
{
tableData = [[NSArray alloc] initWithObjects:#"1", #"2", #"3", #"4", nil];
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"MyCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MyCell"];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UIViewController *anotherVC = nil;
if (indexPath.section == 0) //here I've also tried to use switch, case 0, etc
{
if (indexPath.row == 0)
{
anotherVC = [[ViewForFirstRow alloc] init];
}
NSLog(#"anotherVC %#", anotherVC); // shows that anotherVC = ViewForFirstRow
[anotherVC setModalPresentationStyle:UIModalPresentationFormSheet];
[anotherVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self.navigationController pushViewController:anotherVC animated:YES];
}
}
#end
So, when first row is selected and I didn't link UITableViewController to UIViewController (ViewForFirstRow), it shows a black screen.
If I link UITableViewController to ViewForFirstRow, it opens the view but without the effect I used in code [self.navigationController pushViewController:anotherVC animated:YES];, so it appears that it called the view due to the link and not because of the code [self.navigationController pushViewController:anotherVC animated:YES];
How can I call the view by code properly and what kind of link should I use in storyboard for that? Also, how can I link the UITableViewController to multiples UIViewController (ViewForFirstRow, ViewForSecondRow, ViewForThirdRow, etc).
Thanks!
First, you can't hook up segues from cell (in a dynamic table view) in the storyboard, you have to do it in code. The code should look like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.row == 0) {
UIViewController *anotherVC = [self.storyboard instantiateViewControllerWithIdentifier:#"anotherVC"];
[self.navigationController pushViewController:anotherVC animated:YES];
}else if (indexPath.row == 1) {
UIViewController *someOtherVC = [self.storyboard instantiateViewControllerWithIdentifier:#"someOtherVC"];
[self.navigationController pushViewController:someOtherVC animated:YES];
}
}
You shouldn't be doing anything with indexPath.section since your table view only has one section.
In storyboard presenting another viewcontroller in modally manner then use this:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
Synergy *objSynergy = (Synergy *)[mainStoryboard instantiateViewControllerWithIdentifier:#"Synergy"];
[self presentModalViewController:objSynergy animated:YES];
Synergy is my controller name You can put here your controller name that you want to present.
Make changes with your requirement. And for different viewcontroller open from different row
Use this:
if (indexPath.section==0)
{
if (indexPath.row ==0)
{
}
}

How to Get the Next Class through each section of Uitableview?

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.

Navigating to a new view in iOS

In my iPad application I added UISearchBar programmatically.
When I click on the searchBar, a table containing data appears. When I tap on the row in table, it should navigate to new view
for example:
when I tap on "reliance" it should show a new view. Any suggestions on how I can accomplish this?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ContentController *detailview = [[ContentController alloc] initWithNibName:#"ContentController" bundle:nil];
detailview.detailString = [NSString stringWithFormat:#"%d",indexPath.row];
[self.view addSubView:detailView];
[detailview release];
}
so I added the code above. Do I need to do anything else?
Implement the UITableViewDelegate method didSelectRowAtIndexPath and push your new view onto the navigation stack.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Some code
// Initialize your new view here
[self.navigationController pushViewContoller:yourViewController animated:YES];
}
Try -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ContentController *detailview = [[ContentController alloc] initWithNibName:#"ContentController" bundle:nil];
detailview.detailString = [NSString stringWithFormat:#"%d",indexPath.row];
// [self.navigationController pushViewController:detailview animated:YES];
[self.view addSubView:detailView];
[detailview release];
}

Table view cell disappear when i switch to another page and then back

I have a table view that you can add and delete cells. I can enter multiple cells and when i go to the next page and then switch back, all of my entries/ cells are erased. Can any one figure this out? Here is my code:
#implementation FacePlatesViewController
#synthesize woodGrain;
#synthesize nav, array;
#synthesize EditButton;
#synthesize myTableView, image;
#synthesize cell, string1;
#synthesize myDic, cells, defaults;
#synthesize selectedCell, currentChosenFund;
- (void)viewDidLoad {
[super viewDidLoad];
NSString * myFile = [[NSBundle mainBundle]pathForResource:#"cells" ofType:#"plist"];
self.myTableView.backgroundColor = [UIColor clearColor];
cells = [[NSMutableArray alloc]initWithContentsOfFile:myFile];
}
- (void)addObject:(id)anObject
{
if (anObject != nil)
{
[cells addObject:anObject];
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.myTableView reloadData];
}
- (IBAction)editButton:(id)sender
{
if (self.editing)
{
[self setEditing:NO animated:YES];
[self.myTableView setEditing:NO animated:YES];
}
else
{
[self setEditing:YES animated:YES];
[self.myTableView setEditing:YES animated:YES];
}
}
- (void)add
{
MyDetailViewController * detail = [[MyDetailViewController alloc]init];
detail.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:detail animated:YES];
[detail.text becomeFirstResponder];
[detail release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [cells count];
}
- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[[self cells] removeObjectAtIndex:[indexPath row]];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[[self myTableView] deleteRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationFade];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:CellIdentifier] autorelease];
}
//The if block should end here. You should set the cell's label irrespective whether the cell was nil. This is the cause of the issue you are facing.
cell.textLabel.text = [[cells objectAtIndex:indexPath.row] valueForKey:#"name"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
FirstFolderViewController * first = [[FirstFolderViewController alloc]init];
first.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:first animated:YES];
[first release];
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toIndexPath:(NSIndexPath *)targetIndexPath
{
NSUInteger sourceIndex = [sourceIndexPath row];
NSUInteger targetIndex = [targetIndexPath row];
if (sourceIndex != targetIndex)
{
[[self cells] exchangeObjectAtIndex:sourceIndex
withObjectAtIndex:targetIndex];
}
}
- (void)dealloc
{
[woodGrain release];
[myTableView release];
[EditButton release];
[nav release];
[cells release];
[myTableView release];
[myDic release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[self setWoodGrain:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Thanks :D
Actually even unloading of the view will be enough to throw away the cells. Try this in your viewDidLoad:
if(!cells) {
NSString * myFile = [[NSBundle mainBundle]pathForResource:#"cells" ofType:#"plist"];
self.myTableView.backgroundColor = [UIColor clearColor];
cells = [[NSMutableArray alloc]initWithContentsOfFile:myFile];
}
(this way you won't be rereading the array every time the view is loaded).
And if you want the added cells to persist between app restarts, you do need to save them somewhere. You can't change the files in the main bundle, but you can write your own file into the Caches folder, which you can get via:
[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
Write your file into that folder and read the cells from there instead of the main bundle. If you have some pre-defined cells in the main bundle file, you can check if the file in the Caches folder exists when the app starts, and if not, copy the bundle's file into the Caches folder.
Edit: if you do presentModalViewController to get back from the another page, you'll get a fresh copy of FacePlatesViewController, which obviously loads the default cells from the file.
Instead, you should add a "delegate" property to your FirstFolderViewController:
#property(nonatomic, assign) id delegate; //yes, assign, not retain
then when presenting FirstFolderViewController do:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
FirstFolderViewController * first = [[FirstFolderViewController alloc]init];
first.delegate = self;
first.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:first animated:YES];
[first release];
}
Then add a method to FacePlatesViewController:
- (void) onDoneWithFirstFolderViewController //you can come up with a better name
{
[self dismissModalViewControllerAnimated:YES];
}
and in your FirstFolderViewController, when you are ready to close it, do:
if([delegate respondsToSelector:#selector(onDoneWithFirstFolderViewController)])
[delegate onDoneWithFirstFolderViewController];
Ironically, if you had implemented the cell persistence in a file, this issue might have been unnoticed (because each new FacePlatesViewController would load an up-to-date cell list), but you would have had a memory leak each time you went between pages.
To me, the problem seems to be related to your code in ViewWillAppear: Have you ensured that your cells array is fine when you call [self.myTableView reloadData]; ?
Also, I noticed myTableView. Are you subclassing UITableViewController or implementing table delegates? If you are subclassing, the table view reference is named tableView.
HTH,
Akshay