0

我不确定这是一个糟糕的实现还是一个糟糕的决定,但我试图在我的 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
4

1 回答 1

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
于 2013-08-30T19:25:57.943 回答