2017-04-06 58 views
2

我有一个单元格的表格,每个单元格显示一个单独的对象(链)。我试图为这些单元格中的每一个添加一个按钮,该按钮将该单元格的链作为参数进行调用,或者至少以某种方式将其传递给它。我的代码如下:RubyMotion - 传递参数按钮按

def select_chain(sender) 
    unselect_old_chain 
    chain = Chain.find(sender.tag) 
    chain.selected = true 
    chain.save 
    cdq.save 
end 

def tableView(tableView, cellForRowAtIndexPath: indexPath) 
    cell = tableView.dequeueReusableCellWithIdentifier(ChainCell::ID, forIndexPath: indexPath) 
    cell.label_title.text = data_source[indexPath.row].title 
    cell.chain = data_source[indexPath.row] 
    button = UIButton.buttonWithType(UIButtonTypeCustom) 
    button.setTitle 'Go!', forState: UIControlStateNormal 
    button.tag = cell.chain.id 
    button.addTarget self, action: :select_chain, forControlEvents: UIControlEventTouchUpInside 
    button.backgroundColor= UIColor.greenColor 
    button.frame = CGRectMake(cell.frame.origin.x + 210, cell.frame.origin.y + 10, 80, 20) 
    cell.contentView.addSubview(button) 
    cell 
end 

我已经通过以下阅读:

How to pass parameters via the selector/action?

这就提供了两种解决方案,无论是它的工作对我来说。首先是设置一个实例变量等于变量,但是因为我有多个单元格,我需要多个实例变量,并且会很快变得混乱。

提出另一种解决方案是使用标签属性,我试着在我的代码impliment,然而这将引发错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ChainsTable select_chain]: unrecognized selector sent to instance 0x1169b9000' 

至于我,我只是用看一个整数,试图找到链Chain.find(sender.tag),但是这是行不通的。

任何意见,我怎么能得到这种方法工作或任何其他方法是非常感谢。

在此先感谢。

回答

1

我认为你需要把它写这样的:

button.addTarget self, action: 'select_chain:', forControlEvents: UIControlEventTouchUpInside 

不要使用符号作为选择。