Table View scroll when text field begin editing iphone - iphone

I have table view controller in iphone application. Table view has two sections. First section has two rows and second section has one row. Second section has a custom table view cell.
Second section has a textfield which hides when text field begin editing and keyboard pops up. I want this table view to scroll when keyboard pops up.
I tried the following code which I came across on different websites but in vain.
Thanks in advance.
-(void) textFieldDidBeginEditing:(UITextField *)textField {
CGRect textFieldRect = [textField frame];
[self.tableView scrollRectToVisible:textFieldRect animated:YES];
}
atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
-(void) textFieldDidBeginEditing:(UITextField *)textField {
UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
[self.tableView scrollToRowAtIndexPath:[tableView indexPathForCell:cell]
atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}

I have run into this on Static Cell TVC's. There is an issue when overriding viewWillAppear and NOT calling its Super. So if you are doing that, make sure to call
[super viewWillAppear:animated];
at the top of viewWillAppear

You want to use the setContentOffset method of the table view. Determine the magnitude of the vertical scroll (in pixels), and then:
CGFloat verticalScroll = ... your code here ...
[self.tableView setContentOffset:CGPointMake(0, verticalScroll) animated:YES];

My problem was I was adding the table cell containing the UITextField in the
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
function. If you do this the automatic UITableView scrolling doesn't work.
So, you have to do some arithmetic to work out when your last row is showing and put your special UITableViewCell in here along with all the others.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

Related

How to Hide UITableView?

When pressing a button in my view controller it shows a UIView which has UISearchBar and UISearchDisplayController and UITableView in it(changes the view's hidden property to NO).
When I select a row in the table view I set the view's property to hidden=YES, but it doesn't hide the tableView. What can be the problem?
I implemented the UITableViewDelegate protocol and I receive tableView:didSelectRowAtIndexPath: message(I set the view's hidden=YES there).
Please note that when I call view's hidden=YES from searchBarCancelButtonClicked: it hides successfully.
I think that UISearchDisplayController is causing the problem.
Some code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.searchView.hidden = YES; // hide successfully the tableView.
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.searchView.hidden = YES; //doesn't hide the tableView.
}
EDIT: Found the problem, see my answer below. In short I added [self.searchDisplayController setActive:NO];
Found the problem - I needed to add [self.searchDisplayController setActive:NO]; and now it works.
Code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.searchDisplayController setActive:NO]; //Added line of code.
self.searchView.hidden = YES; // Now it works!
}
Try this:
tableview.hidden = YES;
[self.yourview setHidden:Yes];
if the tableview is inside the view it should also hide

How to animate up UITableView when Keyboard pops up?

I have a UITableView with 10 rows. Each cell has a UITextField in it.
The problem is, when I click a UITextField in a cell at the bottom, the keyboard pops up and blocks the cell. How can I deal with this? Do I need to animate the tableview up or something?
Updated Code:
- (void)textFieldDidBeginEditing:(UITextField *)textField{
self.vitalsTableView.contentInset=UIEdgeInsetsMake(0,0,200,0);
NSIndexPath *path = [self.vitalsTableView indexPathForSelectedRow];
[self.vitalsTableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
use - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated method of UITableView
example:
[tableView scrollToRowAtIndexPath:cellIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
will scroll your tableView to the lowermost possible offset that maintains the cell visible. If your cell does not have enough cells under, you can set a contentInset for your table to allow the scroll offset.
maybe your UITextField delegate is the table view controller. You can make the call on - (void)textFieldDidBeginEditing:(UITextField *)textField
In the case of the bottom cell:
- (void)textFieldDidBeginEditing:(UITextField *)textField{
tableView.contentInset=UIEdgeInsetsMake(0,0,200,0);
[tableView scrollToRowAtIndexPath:cellIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
tableView.contentInset=UIEdgeInsetsMake(0,0,0,0);
}
In case anyone runs into this same problem in iOS 7: I found that this problem was happening to me simply because I forgot to call the super class' implementation of viewWillAppear: in my own viewWillAppear: implementation. One line fixed the problem:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Do whatever you need to
}

iPhone:How to change default delete button's frame in UITableViewCell

I have facing problem is that Is there any way to change default frame
of delete button in UITableView and one more thing is that delete button is always display in center of cell of UITableViewcell so any way to change frame of delete button.
// Update the data model according to edit actions delete or insert.
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[appDelegate.categories removeObjectAtIndex:indexPath.row];
[categoryTableView reloadData];
}
}
#pragma mark Row reordering
// Determine whether a given row is eligible for reordering or not.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
// Process the row move. This means updating the data model to correct the item indices.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath
{
NSString *item = [[appDelegate.categories objectAtIndex:fromIndexPath.row] retain];
[appDelegate.categories removeObject:item];
[appDelegate.categories insertObject:item atIndex:toIndexPath.row];
[item release];
}
Thanks in advance.
in ios 6.1 i was not able to use the above code. I've seen several answers that suggests subclassing the cell class and providing a custom -willTransitionToState. Changing the subview/buttonview frame does not affectively change it's displayed position.
However if you override the layoutSubviews method it will work. Here is the code:
//below i moved the delete button to the left by 10 points. This is because my custom tableview has lef
- (void)layoutSubviews
{
[super layoutSubviews];
for(UIView *subview in self.subviews)
{
if([NSStringFromClass([subview class]) isEqualToString:#"UITableViewCellDeleteConfirmationControl"])
{
CGPoint o = subview.center;
subview.center = CGPointMake(o.x - 10, o.y);
}
}
}
You can use your own view by setting the cell's editingAccessoryView property. This can have whatever frame you like, though you'll need to supply a gradient if you want it to look like the standard delete button.
In the xib file of your custom cell, add a new button of whatever size you like (as long as it is smaller than the cell!). This button should not be a subview of your cell, but a separate item in the file.
Connect this button to the editingAccessoryView outlet of your cell and it will replace the standard delete button.

UITable cell selection in a SplitViewController

I have a UISplitViewController with a Table View for navigation. It's similar to the Mail app. When you click on a table view in portrait mode, the popup hides itself. When you click on the nav bar to get the popup back, the selected item no longer appears selected. How can make this item appear selected without re-selecting the item? (just like in the mail app)
In your viewDidLoad method, do you call
self.clearsSelectionOnViewWillAppear = NO; ?
This is how Xcode's SplitView template does it.
Do you have by any change a
[tableView deselectRowAtIndexPath:indexPath animated:YES];
in your didSelectRowAtIndexPath in the RootViewController ?
I've got a solution that works, but it's frustratingly hacky. I have to call selectRowAtIndexPath twice. It seems that cellForRowAtIndexPath is invalidating the selection made in viewWillAppear. It still needs to be called in viewDidAppear, however, so the view scrolls to the proper position before cellForRowAtIndexPath is called.
- (void)viewWillDisappear:(BOOL)animated
{
NSIndexPath *selected = [self.tableView indexPathForSelectedRow];
_selectedRow = selected.row;
}
- (void)viewDidAppear:(BOOL)animated
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_selectedRow inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//initialize cell code here...
if (indexPath.row == _selectedRow) {
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
}
}
For your table view controller, is -viewWillAppear: called before the pop-up is displayed? If so, you could write it as so:
- (void)viewWillAppear:(BOOL)animated
{
[self.tableView selectRowAtIndexPath:<indexPath>
animated:animated
scrollPosition:UITableViewScrollPositionMiddle];
[super viewWillAppear:animated];
}
Obviously, replace <indexPath> with the proper index path and set the scroll position how you want it. You may also want to pass NO instead of animated to make it appear like it was selectd before the view appeared.

How do I select a UITableViewCell by default?

I have created a UITableView and would like a specific UITableViewCell to appear selected (blue) when the view is loaded.
-(void)viewWillAppear:(BOOL)animated {
// assuming you had the table view wired to IBOutlet myTableView
// and that you wanted to select the first item in the first section
[myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
animated:NO
scrollPosition:UITableViewScrollPositionTop];
}
I'm using this technique to select one of two UITableViewCells so the users knows which cell a UIDatePicker below the table view it will effect. This technique is used by Apple in the calendar app when you create a new event and set the dates.
Be judicious using this method, as selecting the row in this way is something Apple suggests against doing to show a "chosen" state.
Instead, consider setting the cell's accessoryType property to something like UITableViewCellAccessoryCheckmark.
You should put it in viewWillAppear.
[myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
animated:NO
scrollPosition:0];
If you try to select it in cellForRowAtIndexPath:, then it will not take the required style.
Definitely watch out. I'm sure you have a good reason, but look closely at the Human Interface Guidelines document Apple provides. Apps get rejected for not unselecting table rows. I'd encourage you to find the appropriate section of the HIG and see Apple offers any suggestions.
Use this code to select cell by default in table view, indexPath can be vary according to your need
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
[theTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
}
Use the UITableViewCell method
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
Info here.
Sometimes, when you're not in a UIViewController, you have no viewWillAppear, and sometimes, you created your UITableView programmatically.
The easy solution is to implement this delegate method:
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.selectedIndex == indexPath.row) {
cell.selected = YES;
}
}
It's does not work in the cellForRowAtIndexPath because the cell is not yet displayed. and the setSelected method is called just when this one is displayed.