ios - 在 Button Swift 中通过标签发送行和部分

标签 ios swift uitableview button sender-id

我在 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/

相关文章:

ios - iOS 11引入的UINavigationBar大标题 View 如何添加自定义 View

ios - Swift:解码 JSON 响应并将嵌套的 JSON 存储为字符串或 JSON

ios - 在哪里删除 Swift 中 NSNotification 的观察者?

ios - 接收远程通知时打开/关闭振动和声音 ios swift

swift - 表格 View 单元格在滚动 Swift 时改变颜色

ios - 以编程方式自定义 TableViewCell

iOS 32 与 64 位应用程序

ios - 使用枚举来执行函数?

ios - 将版本更改为 Swift 1.2 而不是 Swift 2.1

ios - 用于 iPad 的 UITableViewCell