我在 cellForRowAtIndexPath 里面有这个
cell.plusBut.tag = indexPath.row
cell.plusBut.addTarget(self, action: "plusHit:", forControlEvents: UIControlEvents.TouchUpInside)
这个函数在外面:
func plusHit(sender: UIButton!){
buildings[sender.tag].something = somethingElse
}
是否可以发送 indexPath.row
and indexPath.section
或其他替代方案?
谢谢!
编辑
我是这样处理的:
我的自定义按钮
class MyButton: UIButton{
var myRow: Int = 0
var mySection: Int = 0
}
我的自定义单元格
class NewsCell: UITableViewCell{
@IBOutlet weak var greenLike: MyButton!
在 CellForRowAtIndexPath 中
cell.greenLike.myRow = indexPath.row
我在这一行遇到错误。
最佳答案
Is it possible to send the indexPath.row and indexPath.section, or some alternative??
如果您对一个部分中可能的行数施加限制,则可以将两者合并为一个数字。例如,如果您愿意说给定部分中的行数始终少于 1000 行,则可以使用:
cell.plusBut.tag = (indexPath.section * 1000) + indexPath.row
然后使用模和除运算符恢复:
row = sender.tag % 1000
section = sender.tag / 1000
另一种可能性是检查按钮的 super View (以及它的 super View 等),直到找到单元格。获得单元格后,您可以从表中获取该单元格的索引路径。
第三种选择,也许是最好的选择,是让按钮指向单元格而不是其他某个对象。然后单元格可以在 View Controller 或其他对象中触发一个 Action ,使用它自己作为发送者。
关于ios - 在 Button Swift 中通过标签发送行和部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32351018/