开发者

UIView ScrollToTop

I have a UIView that has some elements (UIButtons, UILabels, etc.) to create a "header" which is on top of a UITableView. There is also a toolbar with a segmentedcontrol inside of it. The idea here is that a single view has two "pages" of data, which show different cells within the table, depending on which segmented control buttons are selected.

Since the table stretches off the screen of an iPhone, users will scroll the table, which scrolls the elements in the "header" off the top of the screen (this is desired and intended). However, if the user then taps on the other segmented control button (to switch "pages"), I cannot seem so find a way to programmatically scroll the entire view back to the top so that the UIButtons and UILables, etc. in the "header" at the top (above the table) appear. I can scroll to the top of the table using [tableview ScrollToTop], but what I am looking for is something like [self.view ScrollToTop]... too bad no such thing exists in a UIView.

Does anyone have any idea how I might achieve the desired 开发者_开发知识库effect here? Many thanks...


You should consider either implementing a UIScrollView between your UIView and other elements, or make the table scroll within fixed bounds, so that all the UI elements are visible all the time. You can scroll a UIScrollView by setting the contentOffset to CGPointZero

see: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIScrollView_Class/Reference/UIScrollView.html


Why not set the UIView you're using for your header as the UITableView's tableHeaderView?

tableView.tableHeaderView = yourUIViewHeader;

You could even put your segmented control in there as well, if thats what you're looking for. Personally, I like to always keep segmented controllers visible since they're an important aspect of the navigation. So I would place it in the view of your controller above your table.

Like Jeremy said, you can set the scroll position with contentOffset. If you have two "pages" of content or two datasources sharing one table, you can fake the appearance of two tables more convincingly by storing the offset of each "page" as instance variables (CGPoints) and setting the table's contentOffset for the selected page when the user switches segments. This way scroll position is maintained across "pages".

- (void) yourSegmentedControlAction {
  switch (self.yourSegmentedControl.selectedSegmentIndex) {
    case 0: // Page 1
      self.pageTwoOffset = self.tableView.contentOffset;
      // set your page one datasource
      self.tableView.contentOffset = self.pageOneOffset;
      break;
    case 1: // Page 2
      self.pageOneOffset = self.tableView.contentOffset;
      // set your page two datasource
      self.tableView.contentOffset = self.pageTwoOffset;
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜