1

我正在阅读有关 RubyMotion 中子视图控制器的教程。(我想做的是在 Instagram 的个人资料页面中创建一个选项卡式界面。)我的工作正常,但我的问题是我的容器有一个滚动视图,而我添加的孩子是一个 tableView,它是本身就是一个滚动视图。这两个滚动相互独立,我希望它们作为一个滚动。鉴于下面的代码,有没有办法可以做到这一点?

我的容器:

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

共享代码:

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
4

1 回答 1

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
于 2013-08-16T14:53:10.523 回答