how to add programmatically drop down menu in navigation bar? - iphone

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

Related

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

how to Navigate from one cell to next view?

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

How to generate a UITextField and UIButton in UIScrollView and remove these items dynamically

HI stackoverflow friends
I have situation that when i click on a button it will generate dynamically a uitextfield with a remove button. If I don't need that text field then i click the remove button it will goes off.More over the user can generate infinite no: of these pair. So that I think it should be add in a scrollview. Is it possible this. Can anyone know how to do this?
Any help would be appreciable.
In your TableView cellForRowAtIndexpath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITextField* textField;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
textField = [[UITextField alloc] initWithFrame:CGRectMake(10,20,30,30)];
textField.tag = 10;
[[cell contentView] addSubView:textField];
[textField release];
}
else
{
textField = (UITextField *)[cell.contentView viewWithTag:10];
}
// Do something with TextField
return cell;
}
You can just have one add button on the navigation bar across top right that would add to NSMutableArray addObject textField to it and reloadTable. Inside cellForRowAtIndexPath get objectAtIndex for that array and add to cell contentView. To delete the cells you can implement swipe to delete for cells...
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// If row is deleted, remove it from the Array.
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// delete your data item here
}
}
Posted some code that should help you:
// alloc + init + set appropriate size and origin
UIScrollView *scroll;
UITextField *field;
UIButton *button;
// add subview
[scroll addSubview:field];
[scroll addSubview:button];
// remove subview
[field removeFromSuperview];
[button removeFromSuperview];
// memory managment
[field release];
[button release];

add TableView subview crashes app

I am trying to add a tableview so when someone press a certain button, the view would switch into tableview with a few choices.
Here is my code for the button:
-(IBAction)buttonPressed:(id)sender
{
LevelChoice *level = [[LevelChoice alloc] initWithNibName:nil bundle:nil];
[self.view addSubview:level.view];
[level release];
}
Here are code snap from my subclass of UITableViewController:
LevelChoice.h
Code:
#interface LevelChoice : UITableViewController {
NSArray *choices;
}
LevelChoice.m
Code:
-(void)viewDidLoad
{
choices = [[NSArray alloc] initWithObjects:#"Level 1", #"Level 2", #"Level 3", nil];
[super viewDidLoad];
}
Code:
-(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.text = [choices objectAtIndex:indexPath.row];
return cell;
}
Does anyone know what am I missing?
Distinguish between Controllers and Views. You can
either present the UITableViewController with presentModalViewController:animated: or with pushViewController:animated:. (Yes, in this case you can release it.)
or just keep a UITableView in your existing view controller and show or hide it as necessary with the hidden property. Of course you need to implement the datasource and delegate methods for the table.

table view containg image view

i ve got an imageview ..below the imageview is a table view..which loads the data from server.by default five fields in table view are visible ..works fine..if i m to scroll the table view to view the remaining fields..the scroller rebounces..wat i want is the table view to be fixed and not to rebounce when scrolled ..i d be so greatful if u guys cud help me out..below is the code..
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [items count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 28.0;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,
tableViewteam.bounds.size.width, 28)] autorelease];
headerView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"team"ofType:#"png"]]];
return headerView;
}
- (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.selectionStyle=UITableViewCellSelectionStyleNone;
cell.imageView.backgroundColor=[UIColor clearColor];
cell.textLabel.text=[[items objectAtIndex:indexPath.row]objectForKey:#"title"];
cell.textLabel.textColor=[UIColor whiteColor];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
I didn't fully understand your problem. If you want to stop bouncing the table view you can add
yourTableView.bounces = NO;
in ViewDidLoad()
If you need your tableView to be fixed you can disable scrolling.
tableView.scrollEnabled = NO;