在使用addChildViewController添加后,使tableView与父scrollView一起滚动

时间:2013-08-10 02:03:45

标签: ios uiscrollview rubymotion childviewcontroller

我正在通过tutorial on child view controllers in RubyMotion。 (我正在尝试做的是在Instagram的个人资料页面中创建一个标签式界面。)我的工作正常,但我的问题是我的容器有一个scrollView,我正在添加的是一个tableView,这是本身就是一个scrollView。两个滚动彼此独立,我希望它们滚动为一个。鉴于我的代码如下,有没有办法可以做到?

我的容器:

class ProfileController < SharedController
  def viewDidLoad
    super

    scroll_frame = self.view.bounds

    @scroll = UIScrollView.alloc.initWithFrame(scroll_frame)
    @scroll.bounces = true
    @scroll.delegate = self
    @scroll.alwaysBounceVertical = true
    @scroll.contentSize = CGSizeMake(UIScreen.mainScreen.bounds.size.width, scroll_frame.size.height)

    main_container = UIViewController.alloc.init
    self.push(main_container)
  end

  def tap_child(sender)
    set_active_tab(sender)
    controller             = ChildPartialController.alloc.initWithNibName(nil, bundle:nil)
    controller.data        = @data[:child]
    controller.parent      = self
    controller.scroll_view = @scroll
    self.push(controller)
  end
end

孩子:

class ChildPartialController < SharedController
  attr_accessor :data, :scroll_view, :parent

  def viewDidAppear(animated)
    scroll_view.contentSize = CGSizeMake(UIScreen.mainScreen.bounds.size.width, 272)
  end

  def viewDidLoad
    super

    self.title = "Child"

    @data = data

    @table = UITableView.alloc.initWithFrame(
      CGRectMake(0, parent.tab_bar.frame.origin.y + parent.tab_bar.frame.size.height, self.view.bounds.size.width, self.view.bounds.size.height),
      style: UITableViewStyleGrouped
    )
    @table.autoresizingMask = UIViewAutoresizingFlexibleHeight
    @table.rowHeight        = 30

    parent.view.addSubview(@table)

    @table.dataSource = self
    @table.delegate = self
  end
end

SharedCode:

class SharedController < UIViewController
  def push(to_vc, animated = false)
    from_vc = self.stack[-1] # could be nil

    self.stack.pop if self.stack.count > 1

    self.stack.push(to_vc)

    self.addChildViewController(to_vc)

    post_animate = lambda { |finished|
      to_vc.didMoveToParentViewController(self)
      to_vc.viewDidAppear(animated)
    }

    if !from_vc.nil?
      to_vc.view.frame = child_frame
      to_vc.viewWillAppear(animated)
    end

    @scroll.addSubview(to_vc.view)
    post_animate.call(true)

    p self.stack.count
  end

  def child_height
    self.view.bounds.size.height - 232
  end

  def child_width
    self.view.bounds.size.width
  end

  def child_frame
    CGRectMake(0, self.tab_bar.frame.origin.y + self.tab_bar.frame.size.height + 10, child_width, child_height)
  end

  def offscreen_top_frame
    CGRectMake(0, 50 - child_height, child_width, child_height)
  end

  def offscreen_bottom_frame
    CGRectMake(0, self.view.bounds.size.height, child_width, child_height)
  end
end

1 个答案:

答案 0 :(得分:0)

事实证明,我觉得我只是用这种方法让事情太复杂了。相反,我将我的子视图控制器转换为UIView的子类,向其添加了一个UITableView,然后将UIView作为子视图添加到父视图的滚动视图中。这是一个例子,如果有人有兴趣:

我的容器:

class ProfileController < SharedController
  def viewDidLoad
    super

    scroll_frame = self.view.bounds

    @scroll = UIScrollView.alloc.initWithFrame(scroll_frame)
    @scroll.bounces = true
    @scroll.delegate = self
    @scroll.alwaysBounceVertical = true
    @scroll.contentSize = CGSizeMake(UIScreen.mainScreen.bounds.size.width, scroll_frame.size.height)
  end

  def tap_child(sender)
    set_active_tab(sender)
    controller             = ChildPartial.alloc.initWithFrame(CGRectMake(0, self.tab_bar.frame.origin.y + self.tab_bar.frame.size.height, self.view.bounds.size.width, self.view.bounds.size.height))
    controller.data        = @data[:child]
    controller.parent      = self
    controller.scroll_view = @scroll
    controller.build_table
    @scroll.addSubview(controller)
  end
end

儿童

class ChildPartial < UIView
  attr_accessor :data, :parent, :scroll_view

  def initWithFrame(frame)
    super

    self
  end

  def data=(data)
    @data = data
  end

  def parent=(parent)
    @parent = parent
  end

  def scroll_view=(scroll_view)
    @scroll_view = scroll_view
  end

  def build_table
    @table = UITableView.alloc.initWithFrame(self.bounds, style: UITableViewStyleGrouped)
    @table.autoresizingMask = UIViewAutoresizingFlexibleHeight
    @table.userInteractionEnabled = false

    @table.dataSource = self
    @table.delegate = self

    resize_scroll_view

    self.addSubview(@table)
  end

  def resize_scroll_view
    @scroll_view.contentSize = CGSizeMake(parent.view.bounds.size.width, parent.view.bounds.size.height + @table.bounds.size.height)
  end

end