Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView automatically scrolls down after a reloadData

I have a table view that is automatically scrolling down a bit in my iPad app (OS X 10.7.3, Xcode 4.3.2), and not sure where to look for the cause. Here is the situation:

The UITableView fills the whole screen, and most of the cells are fixed height cells. The one exception is a cell that contains a UITextView, and is of dynamic height (or in other words, the height of the cell adjusts to fit the amount of text).

The problem manifests itself if the table happens to have the dynamic height cell be cut off when the table view is at the top. When the view first loads, the table is drawn as normal, with just part of the UITextView cell visible. However, if I display a UIPopoverController, I need to do a reloadData after the popover is dismissed, and when I do, the table view will scroll down just enough to show all of the UITextView. (Not the cell containing the UITextView mind you, just the UITextView. The table is a grouped table, and the UITextView cell is the only cell in the section, so the rounded bottom edge is still not visible after this phantom scroll.)

I can't see any code in my app that is scrolling the table view or setting a content offset, so I am totally confused as to what could be causing this.

To try and narrow it down, I set up my view to honor the UIScrollViewDelegate, and if I put a breakpoint in the scrollViewDidScroll: method, the program does in fact stop after the popover goes away and gives me the following backtrace:

(lldb) bt
* thread #1: tid = 0x1f03, 0x0005245f MyApp`-[ContactDisplayViewController scrollViewDidScroll:] + 31 at ContactDisplayViewController.m:1143, stop reason = breakpoint 3.1
    frame #0: 0x0005245f MyApp`-[ContactDisplayViewController scrollViewDidScroll:] + 31 at ContactDisplayViewController.m:1143
    frame #1: 0x017e3494 UIKit`-[UIScrollView setContentOffset:] + 521
    frame #2: 0x0180804d UIKit`-[UITableView setContentOffset:] + 334
    frame #3: 0x017d495e UIKit`-[UIScrollViewScrollAnimation setProgress:] + 523
    frame #4: 0x0183d234 UIKit`-[UIAnimator(Static) _advance:] + 255
    frame #5: 0x02c013a3 GraphicsServices`HeartbeatTimerCallback + 35
    frame #6: 0x023278c3 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
    frame #7: 0x02328e74 CoreFoundation`__CFRunLoopDoTimer + 1220
    frame #8: 0x022852c9 CoreFoundation`__CFRunLoopRun + 1817
    frame #9: 0x02284840 CoreFoundation`CFRunLoopRunSpecific + 208
    frame #10: 0x02284761 CoreFoundation`CFRunLoopRunInMode + 97
    frame #11: 0x02bfe1c4 GraphicsServices`GSEventRunModal + 217
    frame #12: 0x02bfe289 GraphicsServices`GSEventRun + 115
    frame #13: 0x017a4c93 UIKit`UIApplicationMain + 1160
    frame #14: 0x0000263d MyApp`main + 125 at main.m:14

And if I print out the scrollView in that method, it shows that the UITableView is in motion (not sure where the contentOffset is coming from):

Printing description of scrollView:
<UITableView: 0xc3ed400; frame = (0 99; 383 817); clipsToBounds = YES; 
autoresize = W+RM+H; layer = <CALayer: 0x7bf1a40>; contentOffset: {0, 14}>

This phantom scrolling is showing up on the device and the simulator, and in iOS 4.3, 5.0, and 5.1. Does anyone have any ideas?

like image 818
BP. Avatar asked Apr 06 '12 18:04

BP.


2 Answers

When a UITextView becomesFirstResponder (as of iOS 3 I believe), if it is inside a UIScrollView/UITableViewController, that scrollview will be scrolled automatically by iOS to make sure the UITextView is visible.

For a workaround, have a look at: Disable UIScrollView scrolling when UITextField becomes first responder

like image 30
jverdi Avatar answered Oct 23 '22 00:10

jverdi


I have a vague recollection of hearing something about this before. Since UITableViews are a UIScrollView subclass and so are UITextViews it can get all muddled up.

At a guess perhaps try changing some properties on the TextView before reloading, such as disabling scrolling?

[myTextView setScrollEnabled:NO];
like image 131
GregularExpressions Avatar answered Oct 23 '22 00:10

GregularExpressions