scroll table view on selecting a textfield - iphone

i have a tableview with a text field on each cell the problem is tableview should scroll up on selecting the text field.here i'm attaching my project can u please let me know what mistake i had made.
here is the link u can download it from here http://pastelink.me/dl/9c2f89
code that i have used to scroll up.
[super viewWillAppear:YES];
UITableViewCell *cellC = (UITableViewCell *)[[textField superview]superview];
[tabController scrollToRowAtIndexPath:[tabController indexPathForCell:cellC] atScrollPosition:UITableViewScrollPositionTop animated:YES];

look at this project here
add this class to your project and try to use it it may work(TPKeyboardAvoidingTableView .h/TPKeyboardAvoidingTableView .m).

You are using the code,
[tabController scrollToRowAtIndexPath:[tabController indexPathForCell:cellC] atScrollPosition:UITableViewScrollPositionTop animated:YES];
allow the tableView to scroll only when the content size is greater than the view height..
The above code will works if you give higher row count For eg: 15

Give tag to ur text filed properly in cellForRowAt:
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:[textField convertPoint:textField.frame.origin toView:self.tableView]];
when the textField becomes active, use scrollToRowAtIndexPath:atScrollPosition:animated:
for scroll the tableview

Related

Scrolling to a certain custom cell when loading the table

I'm bulding a celander application and i want the calender to get scrolled to the current month when started. i used custom cell for each month. the code that i found to make this happen from this website is this:
[self.bahraincld reloadData];
NSIndexPath *scrollto = [NSIndexPath indexPathForRow:2 inSection:1];
[self.bahraincld scrollToRowAtIndexPath:scrollto
atScrollPosition:UITableViewScrollPositionTop animated:YES];
I'm using it in the view did load. it's giving me a signal SIGABRT.
is this code correct? where should i use it if i want it to work whenever the user starts the calendar.
Thanks.
You better put this code into viewDidAppear, because your tableView haven't loaded the data yet, so the cells are not created. In your case SIGABRT, is because row number 2 is beyond bounds.
Here is what i did, hop it helps someone.
//in the view will appear reload your table.
-(void)viewWillAppear:(BOOL)animated
{
[self.yourtable reloadData];
}
//in the view did appear, value = defining which costume cell to move to.
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
int value = 5;
NSIndexPath *scrollto = [NSIndexPath indexPathForRow:value inSection:0];
[self.yourtable scrollToRowAtIndexPath:scrollto
atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

UISearchBar is hidding in ipad where as in iphone it is working properly

I have designed an application for iphone/ipad. My problem is that I am using single RootViewController for showing Category and Subcategory.
//Pop me to RootViewController Category Section From SubCategory in iphone/ipad
[self.navigationController popViewControllerAnimated: YES];
But in ipad my searchbar get hidden under the navigation bar.Dont know what to do.want to show my searchbar at the same place as it was placed beneath navigation bar ContentOffset is 44.
-(void)viewWillAppear
Just got The Solution
First when the canel button is pressed reload the table
[self.tableView reloadData];
then at which index in the table you wanna to go from secondviewcontroller to firstviewcontroller.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
Necessary if selecting row in -viewDidLoad
[self.tableView selectRowAtIndexPath: indexPath animated: YES scrollPosition: UITableViewScrollPositionTop];

how to create table view who's 1st cell look like as selected?

I have created one ipad app where 1st screen is split view. In that master view is table view and detail view holds image view controller. My problem is that when initially application gets loaded the 1st cell of table view should seen as selected and corresponding image view should appear at detail view.
It seems like you don't need only the cell to be seen as selected, but you need that cell to be selected (to make corresponding image appear)
You should see this post How do I select a UITableViewCell by default?
Here is sample code
-(void)viewDidLoad:(BOOL)animated {
[uiTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:0];
}
or if you actually want to make the cell look like selected, you can do something like
-(void)viewDidLoad:(BOOL)animated {
UITableViewCell *cell= [uiTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
[cell setHighlighted:TRUE animated:TRUE];
}

No scrolling occurs when UITableView scrollToRowAtIndexPath is invoked

My view contains a simple TableView with 6 rows and a button that invokes doScroll when clicked. My objective for doScroll() is simply scroll to the 5th cell such that it's at top of the table view.
- (void)doScroll: (id)sender
{
NSIndexPath *index = [NSIndexPath indexPathForRow:4 inSection: 1];
[m_tableView scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
However when doScroll is invoked, the tableview scrolls only slightly. I suspect this has something to do with the size of the scroll view. So I tried increasing the height of m_tableview.contentsize before scrolling. After I do this however, no scrolling occurs at all...
The view controller is a simple UIViewController and refers to the table view via IBOutlet. For some reason, scrolling works as expected for the default Navigation Based Application, where the controller is a UITableViewController
The UITableView will prevent you from scrolling up empty space. Add enough valid content to the bottom of the table so the cell can scroll up. Use the bounds of the table less the height of the cell for a more accurate frame:
myTable.tableFooterView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)] autorelease];
You could also just use the bottom of the contentInset property.

UITableView scrolling to specific position

I am using iPhone SDK 3.1.3. I have a UITableViewController which gets data from another controller. The table view is added as a subview to the mainview but the frame is set so that it is not visible. The table view frame is updated and made to slide over the main view by tapping on a button.
The table view appears and I scroll to the last row. If I select the last row, I reload the table with more data. The table gets updated with more data. Everything works fine except the scroll position is always the top.
I need the scroll position to be the last row that I clicked on to load more data. I save the scroll position and call the following code after it loads more data. It executes without issues but the scroll position is always the top.
[theTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:savedScrollPosition inSection:0] atScrollPosition:savedScrollPosition animated:NO];
The above seems to have no effect. ViewWillAppear: ViewDidAppear: does not fire and I am told that if the view controller is instantiated in code, which is the case, these don't fire. Please help me figure out how and when to set the scroll position after the table is reloaded ([theTableView reloadData]) so that it is at the row that I clicked on.
Code to Reload table view & scroll
////performAction will notify the tableviewcontroller which will result in didPerformAction being called
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == lastRow)
{
savedScrollPosition = lastRow;
//perform the action
[controller performAction];
}
}
- (void) didPerformAction:(NSNotification *)obj
{
[theTableView reloadData];
[theTableView
scrollToRowAtIndexPath: [NSIndexPath indexPathForRow:savedScrollPosition inSection:0]
atScrollPosition:UITableViewScrollPositionBottom
animated:NO];
}
This seemed to do the trick.
[theTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:savedScrollPosition inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
CGPoint point = theTableView.contentOffset;
point .y -= theTableView.rowHeight;
theTableView.contentOffset = point;
It will look better and the scroll position will stay fixed if you can insert the rows instead of calling reloadData.
[theTableView beginUpdates];
[theTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
// make sure the dataSource will return new rows before calling endUpdates
[theTableView endUpdates];
Instead of using UITableView scrolling you can use UIScrollView scrolling:
savedOffset = [theTableView contentOffset];
Then restore:
[theTableView setContentOffset:savedOffset];
If that's the actual code and assuming theTableView is not nil there, you should be getting a warning saying "may not respond to..." because scrollToRowAtIndexPath is spelled wrong.
Secondly, the atScrollPosition parameter expects a UITableViewScrollPosition enum value indicating where on the screen you want the target row to be positioned.
Try this:
[theTableView scrollToRowAtIndexPath:
[NSIndexPath indexPathForRow:savedScrollPosition inSection:0]
atScrollPosition:UITableViewScrollPositionBottom
animated:NO];