将UIScrollView添加到表格单元格似乎会影响表格滚动性能

时间:2013-08-29 19:01:06

标签: ios uitableview uiscrollview rubymotion

我不确定这是一个糟糕的实现还是一个糟糕的决定,但我试图在我的UITableView中的每个单元格中添加一个水平的UIScrollView。我添加它作为单元格的contentView的子视图,然后调用cell.clipsToBounds = true(仅供参考,我正在使用RubyMotion)。当我点击单元格上的按钮时,我使用beginUpdates/endUpdates触发heightForRowAtIndexPath来展开单元格的高度并显示“隐藏”的滚动视图。

无论如何,因为这样做我的桌子的滚动有点迟钝。我在下面添加了我的代码,并想知道我的实现是否可以改进以修复滚动性能。任何帮助/见解都是有帮助的(尽管如果可能的话我想尝试保留滚动视图)。

my_array = [
  ["Hi", "there", "one"],
  ["Hi", "there", "two"],
  ["Hi", "there", "three"],
  ["Hi", "there", "four"],
]

scroll_frame = CGRectMake(
    0, rowHeight - 1, hidden_width, 51
  )
hidden_scroll = UIScrollView.alloc.initWithFrame(scroll_frame)
hidden_scroll.alwaysBounceHorizontal = true
hidden_scroll.bounces = true
hidden_scroll.contentSize = CGSizeMake(hidden_width * my_array.size, scroll_frame.size.height)
hidden_scroll.delegate = parent
hidden_scroll.clipsToBounds = true
hidden_scroll.pagingEnabled = true
hidden_scroll.showsHorizontalScrollIndicator = false
hidden_scroll.layer.borderWidth  = 1
hidden_scroll.layer.borderColor  = UIColor.colorWithRed(200.0/255, green:200.0/255, blue:200.0/255, alpha: 1).CGColor
hidden_scroll.backgroundColor  = UIColor.colorWithRed(248.0/255, green:248.0/255, blue:248.0/255, alpha: 1)
hidden_scroll.scrollEnabled = false

my_array.each_with_index do |data, index|
  hidden_view = UIView.alloc.initWithFrame(CGRectMake(
      (parent.view.frame.size.width * index), 0, parent.view.frame.size.width, 50
    )
  )

  label_frame           = CGRectMake(
    40, 5, (parent.view.frame.size.width - 40 - 7) * 2/3, 40
  )
  label                 = UILabel.alloc.initWithFrame(label_frame)
  label.font            = UIFont.boldSystemFontOfSize(11)
  label.backgroundColor = UIColor.clearColor
  label.text            = data[0]
  label.numberOfLines   = 0
  label.lineBreakMode   = UILineBreakModeWordWrap
  label_size            = data[0].sizeWithFont(label.font)
  label.sizeToFit
  new_frame             = label.frame
  new_frame.origin.y    = (50 - label.frame.size.height) / 2
  new_frame.size.height = label.frame.size.height
  label.frame           = new_frame

  button = UIButton.buttonWithType(UIButtonTypeCustom)
  button.backgroundColor = UIColor.orangeColor
  button.font = UIFont.systemFontOfSize(11)
  button.setTitle(data[3].upcase, forState:UIControlStateNormal)
  button.setTitleColor(UIColor.whiteColor, forState:UIControlStateNormal)
  button.layer.cornerRadius = 2.0
  button.instance_variable_set(:@data, data)
  button_size = data[3].upcase.sizeWithFont(button.font)
  button.frame = [
    [label.frame.origin.x + label_frame.size.width + 5, (50 - label_size.height) / 2 - 3],
    [80, button_size.height + 6]
  ]

  button.addTarget(parent, action:"add_user_goal:", forControlEvents:UIControlEventTouchUpInside)

  hidden_view.addSubview(label)
  hidden_view.addSubview(button)

  hidden_scroll.insertSubview(hidden_view, belowSubview: cell.contentView)
end

1 个答案:

答案 0 :(得分:0)

由于UIScrollView无论如何都不在视图中,因此当用户点击单元格以查看它时,请尝试仅创建UIScrollView 。在此之前,细胞只有一个空白区域。

对于更干净的代码,我还建议继承UIScrollView并在自定义初始化程序中进行自定义。

class CellScrollView < UIScrollView
  def initWithCustom(args={})
    self.initWithFrame(args[:frame])

    # ... all your customizations. Use the args hash to pass in any other data you need.

    self
  end
end