iPhone UITableView scrolling same time - iphone

I have 2 table view, with the same number of cells.
Could you please point me the correct way, that I could implement simultaneous scrolling. I mean, when I scroll through one table, the other one is scrolling the same time. Thanks.

As uitable view is derived from uiscrollview you can get the scroll amounts in the delegate method
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
You can get content offset of the scrollview which is being scrolled and assign that to the other tableview.
In this method you should be able to access both tableviews.
The property you need to check is contentOffset.
firsttableview.contentOffset = scrollview.contentOffset

Related

iOS UITableView is not scrolling after adding cells

I have a UITableView that represents playable rounds in a game. On first launch, only two cells are shown in the tableView. As the user beats levels, more and more rows are added to the tableView. I reloadData every time the tableView is displayed (which happens each time the user beats a level).
Once the tableView contains enough cells to require scrolling, the tableView will not scroll and where the user left it. It will scroll to reveal that the cells are down below, but when the user releases their finger, the tableView bounces back, not allowing the user to interact with those cells that require scrolling to see. After completing another level and returning back to the tableView, the view will scroll properly.
When the scrolling issue exists, it is as though the tableView does not realize that it is big enough to require scrolling, so it bounces back to its original position, despite the fact that there are cells that are displayed down below when the user attempts to scroll.
I do not implement the heightForRowAtIndexPath or heightForHeaderInSection methods in the tableView's delegate, so that should not be interfering. I have searched this site and Google for similar issues, and have tried a few things including setting the contentSize, but have been unsuccessful.
Is there any reason for this lack of scrolling? Or, more importantly, is there a way to fix it? Thanks!
EDIT:
I see some comments asking about the frame. I logged the frame of the view the table is in, the frame of the tableView itself, and the tableView's content size in the viewDidAppear method of the view the table i in.
Here are the results when the view appears the time before scrolling is necessary. Scrolling behavior is as expected; the user can scroll past the visible area, and it bounces back.
view.frame: {{0, 20}, {320, 460}}
tableView.frame: {{0, 145}, {320, 315}}
tableView.contentSize: {320, 308}
Here are the results when the view appears when scrolling should be necessary. Scrolling will allow the cells below to appear, but when the user releases their finger, it bounces back when it should not.
view.frame: {{0, 20}, {320, 460}}
tableView.frame: {{0, 145}, {320, 315}}
tableView.contentSize: {320, 352}
Note that the contentSize.height did change by 44, as it should have after a new cell was added. The contentSize.height is now larger than tableView.frame.height, but the scrolling behavior does not reflect this.
The protocol UITableViewDelegate conforms to UIScrollViewDelegate, so all you need to do is to implement the methods -scrollViewWillBeginDragging and -scrollViewDidScroll directly in your UITableViewDelegate implementation and they will be called automatically if the implementation class is set as delegate to your UITableView.
So in those methods you can implement logic related to showing content of your tableView. Like suppose you said while scrolling it should show new cell i.e the top most cell must hide at that time. You could write logic in above method as,
//set your content of tableview to show new cell each time.
OR for scroll direction , you have method - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView,
// you can set here start and end coordinates and change them accordingly when scroll decelerates.
Writing your code in such way will not make the scroll bounce back. And you will get perfect results.
Hope this helps.
self.tableview.scrollEnable=YES;//Paste this line where initialized UITableView.

Efficient and smooth scrolling of top view just like in facebook app

I have a top view and a tableview below it. My requirement is while table view is scrolling up, the top view should also scroll up along with the first row. When the table view scrolls down the top view should also scroll along with the current row (not after all rows scrolls down).
Hope I'm clear on above. If anyone has some working code, will appreciate your help.
Thank You.
You can use this delegate for this
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint point =quotesTableView.contentOffset;
//Do your coading in this
}

UITableViewController pull to refresh with a frozen header

I have a mockup that looks like this:
This is all one big Table Controller that you can scroll.
When the section header hits the top of the page it freezes along the top, like so:
Which is working fine. However, when you go back to the top and do a pull to refresh, I want it to do this:
So the pull to refresh dialog appears between the top table header cell and the section header (that is no longer frozen).
Is this possible? I haven't found an implementation like this in my searches.
You can do the following to achieve this,
Add a UIScrollView as the subview of UIViewController's view.
Add a UIView and UITableView as the subview of this scroll view
UIView inside scroll view represents table header cell.
Section header can be the header of UITableview and table contents represents the UITableView's cells.
Add UIRefreshControl as subview of UITableView and set its target method.
Implement the scrollview delegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView for both UITableview and UIScrollview.
When you are scrolling the tableview, move the parent scrollview whenever the table view header moves in the upward direction till the header reaches the top. Add an if condition in scrollview delegate to check for this.
When the table view is moved the downward direction, move the parent scrollview until the UIView table cell header is visible after this stop scrolling of UIScrollview and allow table view to scroll. This will enable the UIRefreshControl.
Here the key thing is the - (void)scrollViewDidScroll:(UIScrollView *)scrollView method and how you implement the scrolling. You can add a check for contentOffset to determine how much tableview and scrollview has been scrolled. In order to restrict the scrolling you can manually set this value in this delegate method to a particular value and it wont scroll after that.

Manually scrolling UITableViewController when a UITextField becomes first responder

Harrr pirates!
I'm trying to make a data entry screen using a UITableViewController. It contains a grouped table view with two sections, each with a few (2 and 4) rows with a UITextField inside them.
When any of the UITextFields become first responder they scroll into view automatically. Great, you would say, but I want to scroll the whole section into view, not just the row containing the UITextField that became first responder.
I don't allow the user to scroll the UITableView by hand and I know to what position it should scroll to display correctly. However, I can't get my UITableViewController to stop automatically scrolling the UITextField into view for me.
Searching StackOverflow I can find a lot of questions about how to resize the UITableView when the keyboard appears, but this doesn't stop the automatic scrolling.
Any suggestions? Your help would be much appreciated!
TableView is basically an extension of scrollView therefore if you want to do the scroll yourself you should use the scrollview API.
Set the contectSize property to the size of the screen without the keyboard and the contentOffset to the location on the table you want to scroll to

Can a UITableView (not UITableViewCell) have variable size?

I have a UIView (created in IB) with a grouped UITableView as a subview. Below this table view is a UIButton. The XIB containing the view will be loaded by a few different viewcontrollers, and so the contents of the table view can vary between one and four cells.
Here's what I want to achieve: when the view loads, the height of the tableview (tableView.frame.size.height) should be adjusted depending on the number of cells, and the button should be placed just beneath the table view.
Can this be done? Could it somehow be done if the view is created programmatically?
Thanks in advance
Edit: Pxl's suggestion was just what I was looking for. A while later, the need arose to have more than just a button below the table view - this was accomplished by creating a separate view containing everything I needed, and implementing the tableView:viewForFooterInSection: and tableView:heightForFooterInSection: functions.
A note for those of you trying to do the same thing: the tableview has to be programmatically created if you want different heights for the footers, or footers for only some of the sections. This is because the footer height set in IB will override the one returned from the tableView:heightForFooterInSection: function.
if there are only a handful of rows, may i suggest that you create a special UITableViewCell that contains just a button?
then make that button cell the bottom row of the last group all the time. make the group so that it will be unlabeled and appear as if the button is sitting at the bottom of your tableview. this way you won't have to muck around with recalculating the tableview's frame and redrawing it.
if the tableview will scroll due to there being many rows, then you'd be calculating the height of the tableview up to a set max (at which point the tableview will need to scroll to show more rows).
once you've determined the height of the tableview you'll need to display your rows, make a frame of the appropriate size, set the tableview's frame to it, position the button just under the tableview, and then redraw the view.
the layout and positioning in this case will need to be done programmatically.
UITableview is a subclass of UIView, so you can change its frame to suit your needs just like a UIView, and UITableView will manage drawing itself to whatever frame you give it.
Just use the methods UITableViewDataSource and UITableViewDelegate provides you.
height = [self tableView:numberOfRowsInSection]*[self tableView:heightForRowAtIndexPath:] + A_CONSTANT_FOR_HEADER_AND_FOOTER_HEIGHT
I agree with pxl that adding a cell with the button in it may be the easiest way to accomplish what you want.
Whether or not you do that, the table view's sizeToFit method should resize the view to (just) fit its contents. If that doesn't work, you can use numberOfSections and rectForSection: to build a loop that determines the height of the table's contents, and reset its frame manually. (And then position the button underneath.)