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.
Related
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
}
I think this is a very common situation, but I can't find how everybody else solves the problem.
I have a UITableView, and when a user taps a cell I push a new view controller (using UINavigationController) on the stack. When the user taps "Back" on the navigation bar, the cell still appears in selected state (i.e. blue background).
I want the background to be blue initially, when the user tapped the cell, but to be turned off when the page is shown again.
you could deselect the cell before or after you have pushed the new viewcontroller.
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[aTableView deselectRowAtIndexPath:indexPath animated:YES];
// create and push new viewController
}
There are two ways you can do the same thing:
Either Reload the whole Table in View will Appear
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[YourTableView reloadData];
}
OR Either
-(void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[YourTableView deselectRowAtIndexPath:indexPath animated:YES];
}
Looking at performance wise second one is best solution to use.
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 {
In my iPhone application I have a search bar and search display controller. When the user types something in the search box, the table view loads and is now visible. When a user clicks on a row, I would like to get rid of the tableView and go back to the view where the user originally clicked the search bar. I have searched all over the documentation, I cannot find how to do this anywhere. I have tried [tableView setHidden:YES]; but when the user clicks on the search bar again the tableView never returns.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
Please can someone point me in the right direction.
you can try smth like
[mySerchDisplayController setActive:NO animated:YES];
Why don't you just remove the table view from the View Hierarchy?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
// update your model based on the current selection
[tableView removeFromSuperview];
}
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.