scroll table view top when selecting an textfield - ios

I Have a UITableView with custom cell with UITextField and buttons on that ,my issue is when ever the user selects some textfields in bottom keybord is hiding that textfield , i tried to scroll up the UITableView by seeing some answers in stackoverflow. but it is not scrolling can any one help me in finding out the mistake made by me please.i have written code for scrolling in textFieldDidBeginEditing: method .textFieldDidBeginEditing: is also firing and executing the code in that but it is not scrolling up.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
// NSLog(#"No OF rows:%d",[contents count]);
return [contents count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
static NSString *cellIdentifier = #"cell";
// Try to retrieve from the table view a now-unused cell with the given identifier.
cell = (uploadCustomCell *)[tableView dequeueReusableCellWithIdentifier:#"uploadCustomCell"];
if (cell == nil) {
cell = [[uploadCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"uploadCustomCell"];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"uploadCustomCell"
owner:self options:nil];
cell = [nib objectAtIndex:0];
}
saveBtnCcell.hidden = YES;
cell.textNamefield.hidden = YES;
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.defaultSwitch setEnabled:NO];
dictionaryContents = [contents objectAtIndex:indexPath.row];
cell
.nameLabelCell.text = [dictionaryContents valueForKey:#"VideoName"];
cell.userName.text = [dictionaryContents valueForKey:#"User"];
cell.thumbImg.image = [arrayimage objectAtIndex:indexPath.row];
NSString *defaultVideo = [dictionaryContents valueForKey:#"DefaultVideo"];
if ([defaultVideo isEqual: #"1"]) {
[defaultSwitche setOn:YES animated:YES];
}
else{
[defaultSwitche setOn:NO animated:YES];
}
[cell.defaultSwitch addTarget:self action:#selector(setState:)forControlEvents:UIControlEventValueChanged];
VideoNameTextField.hidden = YES;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 207;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// selectedRow=indexPath.row;
indexpathTest = indexPath.row;
[tabelView1 reloadData];
NSMutableArray *dictionary = [contents objectAtIndex:indexPath.row];
guid = [dictionary valueForKey:#"GUID"];
detailsVehImg.image = [arrayimage objectAtIndex:indexPath.row];;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
{
return UITableViewCellEditingStyleDelete;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[self.tabelView1 scrollToRowAtIndexPath:1 atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

You should listen to the keyboard notifications and change your table view frame to show the cell you want. Scrolling isn't enough as if the cell is at the bottom it will be hidden anyway
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillDisappear:)
name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillAppear:)
name:UIKeyboardWillShowNotification
object:nil];
}
-(void)viewDidUnload
{
[super viewDidUnload];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)keyboardWillAppear:(NSNotification *)note
{
CGRect tableViewFrame = self.tableView.frame;
// If your table view doesn't end at the bottom of the screen then this calculation of the height wouldn't be enough, you would need to take into consideration the distance between the screen bottom and the table view
tableViewFrame.size.height = tableViewFrame.size.height - [[note.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
CGRect visibleFrame = CGRectZero;
visibleFrame.origin = self.tableView.contentOffset;
visibleFrame.size = tableViewFrame.size;
[UIView animateWithDuration:[[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]
delay:0
options:UIViewAnimationOptionBeginFromCurrentState | [[note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]
animations:^{
self.tableView.frame = tableViewFrame;
}
completion:^(BOOL finished){
[self.tableView scrollRectToVisible:visibleFrame
animated:YES];
}];
}
-(void)keyboardWillDisappear:(NSNotification *)note
{
CGRect tableViewFrame = self.tableView.frame;
tableViewFrame.size.height = tableViewFrame.size.height + [[note.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
[UIView animateWithDuration:[[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]
delay:0
options:UIViewAnimationOptionBeginFromCurrentState | [[note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]
animations:^{
self.tableView.frame = tableViewFrame;
}
completion:nil];
}

Try this
[self.myTableView setContentOffset:CGPointZero animated:YES];
OR
// Table View - Scroll to top
[self.myTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
[self.myTableView reloadData];
Parameter
animated:YES show scrolling animation.
animated:NO display table after scrolling(no animation.)

You have to reload your tableView before scrolling it. I have added one line of code in your textFieldDidBeginEditing. PLease try this.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self.tabelView1 reloadData];
NSArray *indexPathsForVisibleRows = [self.tabelView1 indexPathsForVisibleRows];
NSIndexPath *selectedIndexPath = [indexPathsForVisibleRows objectAtIndex:(indexPathsForVisibleRows.count/2)];
[self.tabelView1 scrollToRowAtIndexPath:selectedIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
}
Good Luck

Related

Replicating the behaviour of iOS Mail compose interface, but view doesn't follow cursor

I've been working on creating a replica of Apple's Mail compose view so that messages can be sent via API calls rather than the standard email sending that iOS uses.
So far everything is going well, however when I type in to the message body the cursor will begin to disappear under the keyboard.
I am not sure if this is related to the way I have the constraints set up or if its something else, but right now it does not work as expect.
Here is the related code I have this far for the view controller...
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.bodyTextView.delegate = self;
UIBarButtonItem *cancelBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelButton:)];
UIBarButtonItem *sendBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Send" style:UIBarButtonItemStyleDone target:self action:#selector(sendButton:)];
self.navigationItem.leftBarButtonItem = cancelBarButton;
self.navigationItem.rightBarButtonItem = sendBarButton;
self.navigationItem.title = #"Compose Email";
self.recipients = [NSMutableArray array];
if (self.contactID) {
[self.recipients addObject:#{#"contact_id":self.contactID}];
} else {
[self.recipients addObject:#{#"name":self.name, #"address":self.emailAddress}];
}
[self prepareTableView];
self.tableView.rowHeight = UITableViewAutomaticDimension;
}
- (void)prepareTableView {
if (!self.sectionTitles) {
self.sectionTitles = [NSMutableArray array];
}
[self.sectionTitles addObject:#"To"];
[self.sectionTitles addObject:#"Subject"];
[self.sectionTitles addObject:#"MessageBody"];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.sectionTitles.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [UITableViewCell new];
NSString *sectionTitle = [self.sectionTitles objectAtIndex:indexPath.section];
if ([sectionTitle isEqualToString:#"To"]) {
EmailToTableViewCell *toCell = [tableView dequeueReusableCellWithIdentifier:#"EmailToCell"];
toCell.selectionStyle = UITableViewCellSelectionStyleNone;
toCell.toTextField.text = self.name;
toCell.toTextField.accessibilityLabel = #"Message Recipient";
cell = toCell;
}
if ([sectionTitle isEqualToString:#"Subject"]) {
EmailSubjectTableViewCell *subjectCell = [tableView dequeueReusableCellWithIdentifier:#"EmailSubjectCell"];
subjectCell.selectionStyle = UITableViewCellSelectionStyleNone;
self.subjectField = subjectCell.emailSubjectTextField;
self.subjectField.accessibilityLabel = #"Message Subject";
if (self.subject) {
self.subjectField.text = self.subject;
}
cell = subjectCell;
}
if ([sectionTitle isEqualToString:#"MessageBody"]) {
self.bodyCell = [tableView dequeueReusableCellWithIdentifier:#"EmailBodyCell"];
self.bodyCell.selectionStyle = UITableViewCellSelectionStyleNone;
self.bodyTextView = self.bodyCell.emailBodyTextView;
self.bodyTextView.accessibilityLabel = #"Message Body";
if (self.messageBody) {
self.bodyTextView.text = self.messageBody;
}
cell = self.bodyCell;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 2) {
return self.bodyTextView.frame.size.height + 8;
} else {
return 44;
}
}
#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
[self.tableView beginUpdates];
[self.tableView endUpdates];
[self scrollToCursorForTextView:textView];
}
- (void)scrollToCursorForTextView: (UITextView*)textView {
CGRect cursorRect = [textView caretRectForPosition:textView.selectedTextRange.end];
cursorRect = [textView convertRect:cursorRect toView:self.view];
cursorRect = [self.tableView convertRect:cursorRect fromView:textView];
if (![self rectVisible:cursorRect]) {
cursorRect.size.height += 16;
[self.tableView scrollRectToVisible:cursorRect animated:YES];
}
}
- (BOOL)rectVisible: (CGRect)rect {
CGRect visibleRect;
visibleRect.origin = self.tableView.contentOffset;
visibleRect.origin.y += self.tableView.contentInset.top;
visibleRect.size = self.tableView.bounds.size;
visibleRect.size.height -= self.tableView.contentInset.top + self.tableView.contentInset.bottom;
return CGRectContainsRect(visibleRect, rect);
}
- (void)keyboardWillShow:(NSNotification*)notification {
NSDictionary* info = [notification userInfo];
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
self.tableView.contentInset = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
}
- (void)keyboardWillHide:(NSNotification*)aNotification {
self.tableView.contentInset = UIEdgeInsetsZero;
}
Here's what I have for the UI...
And finally here's a link to a video showing what happens so far. Note that I've reduced the minimum size of the UITextView that is one of the cells in the UITableView to better get a feel for what's going on.
Link to video of problem
If I have missed anything that might be helpful in figuring out this problem please let me know and I'll add it to the question.
UITableViewController handles this automatically. Try removing all the additional code and calculations. Make sure yourUITextView leading, trailing, top and bottom constraints are set.
If you use UITableView then you have to do all those calculations with keyboard show/hide notifications.

Load UIWebView in UITableViewCell and Make UITabViewCell's height as per the content of UIWebview

I am working on a project, where I have a UITableViewCell containing UIImageView, UILable and UIWebView.
I need to open a webview (with added HTML string in it) on didselectrowatIndexpathmethod and cell height should be increase to the content size of UIWebView. I have tried following code. But this is not working as per the expectation. Kindly advice.
#pragma mark - Table view data source
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [faqContentArray count];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
FAQCell *cell = (FAQCell*)[tableView cellForRowAtIndexPath:indexPath];
[cell.faqContentWebView loadHTMLString:[[faqContentArray objectAtIndex:indexPath.row] objectForKey:#"content"] baseURL:nil];
NSString *wrappedText = [[faqContentArray objectAtIndex:indexPath.row] objectForKey:#"content"];
cell.faqContentWebView.frame=CGRectMake(8.0f, 43.0f, 304.0f, [self minHeightForText:wrappedText]);
[self rotateIconToExpanded:cell.faqImage];
[self.faqTableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
}
//-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
// FAQCell *cell = (FAQCell*)[tableView cellForRowAtIndexPath:indexPath];
//
// [cell.faqContentWebView loadHTMLString:#"" baseURL:nil];
// cell.faqContentWebView.frame=CGRectMake(8.0f, 43.0f, 304.0f, 5);
// [self rotateIconToCollapsed:cell.faqImage];
// [self.faqTableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationTop];
//}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FAQCell *cell = (FAQCell*)[tableView dequeueReusableCellWithIdentifier:#"MyCell"];
if (!cell) {
[tableView registerNib:[UINib nibWithNibName:#"FAQCell" bundle:nil] forCellReuseIdentifier:#"MyCell"];
cell = (FAQCell*)[tableView dequeueReusableCellWithIdentifier:#"MyCell"];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
cell.faqContentWebView.delegate=self;
cell.faqContentWebView.layer.cornerRadius = 0;
cell.faqContentWebView.userInteractionEnabled = NO;
cell.faqContentWebView.multipleTouchEnabled = YES;
cell.faqContentWebView.clipsToBounds = YES;
cell.faqContentWebView.scalesPageToFit = NO;
cell.faqContentWebView.scrollView.scrollEnabled = NO;
cell.faqContentWebView.scrollView.bounces = NO;
cell.faqContentWebView.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
cell.faqTitleLbl.text=[[faqContentArray objectAtIndex:indexPath.row] objectForKey:#"title"];
cell.faqImage.image=[UIImage imageNamed:#"downarow.png"];
return cell;
}
- (void)rotateIconToExpanded:(UIImageView *)iconImage {
[UIView beginAnimations:#"rotateDisclosure" context:nil];
[UIView setAnimationDuration:0.2];
iconImage.transform = CGAffineTransformMakeRotation(M_PI * 2.5);
[UIView commitAnimations];
}
- (void)rotateIconToCollapsed:(UIImageView *)iconImage {
[UIView beginAnimations:#"rotateDisclosure" context:nil];
[UIView setAnimationDuration:0.2];
iconImage.transform = CGAffineTransformMakeRotation(M_PI * 2);
[UIView commitAnimations];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
id celll = [self tableView:tableView cellForRowAtIndexPath:indexPath];
FAQCell *cell=(FAQCell *)celll;
[cell setNeedsLayout];
[cell layoutIfNeeded];
NSLog(#"height------>%f",[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height);
return [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
}
You will need to calculate (and possibly monitor) the size of your content in the UIWebView. One way is through UIWebView's UIScrollView:
CGFloat webViewContentHeight = myWebView.scrollView.contentSize.height;
another is:
CGFloat webViewContentHeight = [myWebView stringForEvaluatingJavaScript:#"document.body.offsetHeight"].floatValue;
When you retrieve that height is important, you will want it once the content of the UIWebView has finished loading (once it has you can invalidate/update the constraints of your UITableViewCell, for example).

UITextFiled don't show clearButton sometimes

I add a simple UITableView to my project,and every cell include a UITextField with clearButtonMode = UITextFieldViewModeAlways.
but strange issue occurs .when I reloadData,some textfield don't show clear button.
so I grab the part and build a sample and never find this issue.
I just wonder is some property has a effect to the textField.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"userIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
for (UIView *sv in cell.contentView.subviews) {
[sv removeFromSuperview];
}
UITextField *tf = [[UITextField alloc] initWithFrame:cell.bounds];
tf.text = [self.tb_data[indexPath.row];
tf.rightViewMode = UITextFieldViewModeAlways;
tf.clearButtonMode = UITextFieldViewModeAlways;
tf.tag = indexPath.row+100;
tf.delegate = self;
[[cell contentView] addSubview:tf];
return cell;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.tag >= 100) {
return NO;
}
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if (textField.tag >= 100) {
[self.tb_data removeObjectAtIndex:textField.tag-100];
[self.tb beginUpdates];
[self.tb deleteRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:textField.tag-100 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
[UIView animateWithDuration:0.3
animations:^{
CGRect frame = self.tb.frame;
frame.size.height = 40*self.tb_data.count;
self.tb.frame = frame;
}];
[self.tb endUpdates];
[self.tb reloadData];
}
return YES;
}

UIPickerView in UITableView

First of all: I know that some people already posted topics like that, but nobody gave a clear answer in all those topics.
I have a settings-UITableView in my application. Now i want to add a TableViewCell where i can choose between some numbers with a UIPickerView.
In the tableView didSelectRowAtIndexPath method i created the if statement for the right cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 1){
}
}
How can i add a UIPickerView for this cell? It should slide up from the bottom and it would be nice if there is something like a "done" button.
Have you seen the sample program released with iOS 7 called DateCell? It uses a UIDatePicker, but I found it pretty straightforward to adapt it to a regular UIPickerView.
It does exactly what you're describing: edit a UITableViewCell with a picker that opens directly under the row you're editing.
This is also based on your other question, so I am including those functionalities here as well (i.e. the switch).
In your YourTableViewController.h
set:
#interface YourTableViewViewController : UITableViewController <UIPickerViewDataSource, UIPickerViewDelegate>
In your YourTableViewController.m
Paste this code:
#interface YourTableViewViewController ()
#property NSInteger toggle;
#property (strong, nonatomic) UIPickerView *pickerView;
#end
#implementation YourTableViewViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.toggle = 0;
self.pickerView = [[UIPickerView alloc] initWithFrame:(CGRect){{0, 0}, 320, 480}];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
self.pickerView.center = (CGPoint){160, 640};
self.pickerView.hidden = YES;
[self.view addSubview:self.pickerView];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 2;
}
- (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.selectionStyle = UITableViewCellSelectionStyleNone;
}
if(indexPath.row == 0)
{
[cell.contentView addSubview:self.mySwitch];
}
if(indexPath.row == 1)
{
cell.textLabel.textColor = [UIColor darkGrayColor];
if(self.toggle == 0)
{
cell.textLabel.text = #"Choose a number";
}
else
{
cell.textLabel.text = #"Cancel";
}
}
// Configure the cell...
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 1)
{
if(self.toggle == 0)
{
self.toggle = 1;
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:YES];
[self bringUpPickerViewWithRow:indexPath];
}
else
{
self.toggle = 0;
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:YES];
[self hidePickerView];
}
}
}
- (void)bringUpPickerViewWithRow:(NSIndexPath*)indexPath
{
UITableViewCell *currentCellSelected = [self.tableView cellForRowAtIndexPath:indexPath];
[UIView animateWithDuration:1.0f
delay:0.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^
{
self.pickerView.hidden = NO;
self.pickerView.center = (CGPoint){currentCellSelected.frame.size.width/2, self.tableView.frame.origin.y + currentCellSelected.frame.size.height*4};
}
completion:nil];
}
- (void)hidePickerView
{
[UIView animateWithDuration:1.0f
delay:0.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^
{
self.pickerView.center = (CGPoint){160, 800};
}
completion:^(BOOL finished)
{
self.pickerView.hidden = YES;
}];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
self.toggle = 0;
[self.tableView reloadData];
[self hidePickerView];
NSLog(#"row selected:%ld", (long)row);
}
- (NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [NSString stringWithFormat:#"%d", row+1];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 10;
}
#end
Tested.
Addendum:
Also, in your storyboard, change the separator for YourTableView to None (looks better in your case here).
Table View Separator:
in viewDidLoad, add:
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
And in bringUpPickerViewWithRow
after:
UITableViewCell *currentCellSelected = [self.tableView cellForRowAtIndexPath:indexPath];
add:
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.tableView setNeedsDisplay];
And lastly, in hidePickerView, add:
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
[self.tableView setNeedsDisplay];
Create custom ViewController that include UIPickerView and Done
button.
Add it as subview on your tableview. (Place it bottom so
that it does not visible).
Once user click appropriate cell, you
can animate picker-view from bottom.
Once user select an item
animate and position it to bottom.
I have done above steps for same purpose. If you want I can send custom controller that include UIPickerView and done button.

How to make an iOS Drop down menu

How do I create a good looking drop down menu in iOS. It should open when the user clicks a button in the navigation bar.
I tried creating a table, but got the error Static table views are only valid when embedded in UITableViewController instances. If I use UITableViewController, then it has to be full width.
Maybe you could try using a CollectionView instead of TableView, and if you need sopport for iOS 4+, PSTCollectionView is a good option :)
What I did is added a Button and on button tap one table view is shown, whenever any row is selected that row title is set to button title.
- (void)addOrganizationButton {
self.organizationButton = [UIUtils createButtonWithFrame:CGRectMake(203,115,451,38)
titleText:#"Organization"
type:UIButtonTypeCustom
normalImage:nil selectedImage:nil
target:self
view:self.registerView
tag:0
selector:#selector(organizationButtonTap)];
self.organizationButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
[self.organizationButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
self.organizationButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
self.organizationButton.layer.borderWidth = 1;
}
//Show table view on button tap
- (void)organizationButtonTap {
self.organizationTable = [[UITableView alloc]initWithFrame:CGRectMake(203,153, 451,220)];
self.organizationTable.layer.borderWidth = 1;
self.organizationTable.dataSource = self;
self.organizationTable.delegate = self;
[UIView animateWithDuration:1
delay:0.0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
CGRect frame = self.organizationTable.frame;
frame.size.height = 220;
self.organizationTable.frame = frame;
}
completion:^(BOOL finished){
NSLog(#"Done!");
}];
[self.registerView addSubview:self.organizationTable];
}
//Table view delegates
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;//to be changed
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
return 3;//get dictionary count
}
- (UITableViewCell *) tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
NSString *cellIdentifier=#"Cell Identifier";
cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = [NSString stringWithFormat:
#"Section %ld, Cell %ld",
(long)indexPath.section,
(long)indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.organizationButton setTitle:#"faf" forState:UIControlStateNormal];
[UIView animateWithDuration:1
delay:0.0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
CGRect frame = self.organizationTable.frame;
frame.size.height = 0;
self.organizationTable.frame = frame;
}
completion:^(BOOL finished){
NSLog(#"Done!");
}];
}
You can prepare a custom view for that and add it as a subview in your ViewController.
Use UIView animations for simulating "drop down" effect.

Resources