how to Navigate from one cell to next view? - iphone

I want my app to have 5 rows and each row has a particular height. Each row has a title, subtitle and an image. Then i want to be able to navigate to the next page when i tap on either of the rows(for e.g say 3rd row). how do i do this?

Answer to your 2nd part of the question is the initWithStyle:UITableViewCellStyleSubtitle…. this one allows you to have a title and subtitle. For the image, each cell has already built-in.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
}
NSString *title = (#"your title");
NSString *subTitle = (#"your subtitle");
cell.textLabel.text = title; //title
cell.detailTextLabel.text = subTitle; //subtitle
NSString *filePath = //file path to your image
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
cell.imageView.image = [myThumbnailClass image]; //image
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row==0)
{
singleProductViewController=[[SingleProductViewController alloc]initWithNibName:#"SingleProductViewController" bundle:nil];
[self.navigationController pushViewController:singleProductViewController animated:YES];
[singleProductViewController release];
}
else if(indexPath.row==1)
{
//Sec view Navigation
}
//Like wise u go on
}

Use method for handle tableview touch
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
You can check indexPath which row taped.
See this tutorial - Navigating a Data Hierarchy With Table Views

Use the tableview delegate method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
singleProductViewController=[[SingleProductViewController alloc]initWithNibName: #"SingleProductViewController" bundle:nil];
[self.navigationController pushViewController:singleProductViewController animated:YES];
[singleProductViewController release];
}
SingleProductViewController is the new view you want to navigate
All the best

Related

Loading Generic view from UITableViewController

Here is my problem :
I created a .xib with some label. I created a UITableViewController like this :
- (void)viewDidLoad {
[super viewDidLoad];
dataToShow = [[NSArray alloc] initWithObjects:#"cave",#"garage",nil];
}
- (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];
}
cell.textLabel.text = [dataToShow objectAtIndex:indexPath.row];
return cell;
}
The numberOfSectionInTableView and numberOfRowInSection are filled correctly, when running the programe the list shows as it should. I would like now, depending on which row the user click in the table view, to load the xib i created and fill the labels with text. Is that possible and if it is, how do I pull it off ? Thanks
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
yourNextViewController *nextViewXib = [[yourNextViewController alloc]initWithNibName:#"yourNextViewController" bundle:nil];
nextViewXib.yourLable.text=[dataToShow objectAtIndex:indexPath.row];
[self.navigationController pushViewController:nextViewXib animated:YES];
[nextViewXib release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"selected row:%d",indexPath.row);
}
//in this mathod you get will row is calling..

how to add programmatically drop down menu in navigation bar?

can anyone tell. how to create drop down menu in iPhone,and i want add drop down menu in navigateion bar (my concept is sorting(filter) so i want three buttons in menu name,title,description .....)
You can use a UIPopoverController for iPhone.
It's available here.
In the popover you can add a UIPickerView and there's the drop-down.
Basically , on iPhone you can use a UITableView or UIPickerView to simulate a drop down.
And to place it in a nice container you can use the above mentioned popover.
There is no such a component in iOS, so you need to create it by yourself.
You can do that by adding a UIView under your button, and animate it.
Something like...
[self.view addSubview:myMenu];
[myMenu setFrame:CGRectMake(100,30,150,0)];
[UIView animateWithDuration:0.4 animation:^{
[myMenu setFrame:CGRectMake(100,30,150,200)];
}];
//on Drop down button click
-(IBAction)btnDropdownPressed:(id)sender{
if (![popoverController isPopoverVisible])
{
PopOverViewController *attShow=[[PopOverViewController alloc]initWithNibName:#"PopOverViewController" bundle:nil];
NSLog(#"arrFiles==%#",arrFiles);
attShow.arrFiles=arrFiles;
{
popoverController=[[[UIPopoverController alloc]initWithContentViewController:attShow] retain];
[popoverController setPopoverContentSize:CGSizeMake(500,250)];
[popoverController presentPopoverFromRect:CGRectMake(0,0, 500, 30) inView:btnMore permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
}else {
[popoverController dismissPopoverAnimated:YES];
}
}
PopOverViewController.h
{
IBOutlet UITableView *tblView;
NSArray *arrFiles;
}
#property(nonatomic,retain)NSArray *arrFiles;
PopOverViewController.m
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section
{
return [self.arrFiles count];
}
-(CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}
- (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];
}
cell.textLabel.text=[self.arrFiles objectAtIndex:indexPath.row];
cell.textLabel.font=[UIFont fontWithName:#"Arial" size:14.0f];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
return cell;
}

tableview is crashing on clicking on cell or on scrolling in ios 5

I made a demo empty app and add a navigation controller with the view
UINavigationController *navBar = [[UINavigationController alloc]initWithRootViewController:objFirstViewControllerViewController];
[self.window addSubview:navBar.view];
After it i add a table view on the first view controller like this .
UITableView* demotableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 400) style:UITableViewStylePlain];
demotableView.delegate = self;
demotableView.dataSource = self;
[self.view addSubview:demotableView];
and the delegate function of the table view and main cell for row function like this way
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.text = #"Hello this is sample text";
cell.textLabel.minimumFontSize = 12;
cell.textLabel.adjustsFontSizeToFitWidth = TRUE;
cell.textLabel.font = [UIFont fontWithName:#"Copperplate" size:18];
return cell;
}
But when i scroll on my table or click any cell to go on the next view it just crash and give these two error on clicking and scrolling respectively.
[__NSCFArray tableView:didSelectRowAtIndexPath:]
[__NSCFDictionary tableView:cellForRowAtIndexPath:]
I don't understand what is getting wrong this code it have been working with the prior os properly
Any body can help please ?
Here is the code for did select row
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Second *anotherViewController = [[Second alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:anotherViewController animated:YES];
}
and no of row is this one
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 15;
}
Actually I misused the ARC what are the changes I made to make the app run successfully actually it was crashing due to memory leak I reference the class in delegate with local object but the it got released when it tried to add the data on it and when table's delegate and datasource try to add the things in the current class it was released and it throw message from those message instances, I was stuck because I was thinking it is happening due to me having taken an empty kind of application but after adding the lines below in the delegate class I got the problem solved.
What I did in the delegate class in the .h file:
FirstViewControllerViewController *objFirstViewControllerViewController;
#property (strong, nonatomic) FirstViewControllerViewController *objFirstViewControllerViewController;
Then my table started behaving properly and all things that I was having problem in.
Replace your code by this and try:
- (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];
}
cell.textLabel.text = #"Hello this is sample text";
cell.textLabel.minimumFontSize = 12;
cell.textLabel.adjustsFontSizeToFitWidth = TRUE;
cell.textLabel.font = [UIFont fontWithName:#"Copperplate" size:18];
return cell;
}
Hope this helps.
Please try this....
add your table view like this..
productList = [[UITableView alloc] initWithFrame:CGRectMake(0,102, 320, 267) style:UITableViewStylePlain];
productList.delegate = self;
productList.dataSource = self;
productList.backgroundColor = [UIColor clearColor];
[self.view addSubview:productList];
and add these methods....
#pragma mark Table view data source
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return (your row count);
}
// 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:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
}
// Configure the cell.
cell.textLabel.text = #"Title";
cell.detailTextLabel.text = formattedString;
cell.detailTextLabel.textColor = [UIColor darkGrayColor];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Second *anotherViewController = [[Second alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:anotherViewController animated:YES];
}
Since you have set demotableView.delegate = self;
you have to implement the tabelView: didSelectRowAtIndexPath: function which solves the crash on selecting(on Click) a Cell.
To resolve the crash of scrolling, you hav to implement
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
method.
P.S: In the cellForRowAtIndexPath:, all the lines except cell.textLabel.text should be inside
if(cell == nil){
}
and please follow proper maemory management rules

Other view is not loading in iPhone app

I am using below code in implementation file. The issue is when I click on a row it does not take me to another view?
please help
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;
}
-(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];
}
cell.textLabel.text = [NSString stringWithFormat:#"Vehicle List %d", [indexPath row]+1];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.vehic == nil) {
Vehicle *vehicle = [[Vehicle alloc] initWithNibName:#"Vehicle" bundle:[NSBundle mainBundle]];
self.vehic = vehicle;
}
[self.navigationController pushViewController:self.vehic animated:YES];
}
For going to another View you should handle this method Correctly :-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// on click on any row now it will go to Another View, let the other view be SecondViewController's View
SecondViewController *viewController = [[SecondViewController alloc]initWithNibName:#"FirstViewController" bundle:nil];
[self presentModalViewController:viewController animated:YES];
}
That's all. If you face any problem then tell me. :)

how to get text of cell from table in to string

i have 2 views and both are table views
1st view
list is there, so when user click on particular cell, i want to take the text of selected cell and store into variable and push 2nd view
e.g. stringVariable = cell.text
2nd view
now i want to set title of view by using stringVariable variable
e.g. self.title = stringVariable;
i hope some one know this issue
thank you in advance
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Save text of the selected cell:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
stringVariable = cell.textLabel.text;
// load next view and set title:
MyView *nextView = [[MyView alloc] initWithNibName:#"MyView" bundle:nil];
nextView.title = stringVariable;
[self.navigationController pushViewController:nextView animated:YES];
}
You must be using some array data to fill the table Views.
eg.
- (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];
}
TableItem * tableItem = [tableItems objectAtIndex:indexPath.row];
UILabel mainLabel = [[UILable alloc] initWithFrame:()];
mainLabel.text = tableItem.name;
[cell.contentView addSubView:mainLabel];
[mainLabel release];
return cell;
}
Similarly you should use the same array to get the required item, which is selected
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
TableItem * tableItem = [tableItems objectAtIndex:indexPath.row];
OtherViewController * otherViewController = [[OtherViewController alloc] init];
otherViewController.name = tableItem.name;
[self.navigationController pushViewController:otherViewController animated:YES];
[otherViewController release];
}