【发布时间】:2016-02-03 13:02:14
【问题描述】:
在我的UITableViewCell 中有一个按钮。我想通过在cellForRowAtIndexPath 方法中传递多个参数来为其添加操作。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CartCell", forIndexPath:
indexPath) as! CartTableViewCell
cell.buyButton.addTarget(self, action: self.buyButton(indexPath, 2, 3 ,4 , 5, 6), forControlEvents: .TouchUpInside)
}
【问题讨论】:
-
不,你不能这样做。该操作不是闭包,它是一个选择器,选择器必须采用 0 或 1 个参数,即发送者按钮。与其在
cellForRowAtIndexPath中向按钮添加操作,不如定义一个协议并将视图控制器设置为单元格委托。 -
你能举个例子吗
-
您可以使用此答案中提供的有关 UIButton 并将多个参数传递给其选择器的解决方案:stackoverflow.com/a/53779104/5324541
标签: ios swift uitableview