【问题标题】:Send multiple tags with UIButton in UITableView在 UITableView 中使用 UIButton 发送多个标签
【发布时间】:2015-10-04 20:07:17
【问题描述】:

我正遭受向一个函数发送两个标签以显示UIActivityViewController 并共享单元格数据的痛苦,远在我曾经一次发送一个值并在单个UIButton 内发送一个值之前:

cell.sharefb.tag = indexPath.row
cell.sharefb.addTarget(self, action: "showAlert:", forControlEvents:UIControlEvents.TouchUpInside)

但现在我已经在我的UITableView 中实现了部分,所以我的数组就像:

Array[indexPath.section][indexPath.row]

其中一个(部分或行)一次不够,我需要将两者都发送到我的函数?我该怎么做?

func showAlert(sender:AnyObject){
    // i want to use it like :
    Array[sender.sectionvalue][sender.rowvalue]
}

【问题讨论】:

标签: ios swift uitableview uibutton


【解决方案1】:

您可以继承 UIButton 并为您需要的数据添加属性,例如

class MyButton : UIButton {

var row : Int?
var section : Int?

}

然后您可以设置这些属性,并在您的 showAlert 函数中将它们取回并使用:

 func showAlert(sender:AnyObject){

    let theButton = sender as! MyButton

    let section = theButton.section
    let row = theButton.row

    }

编辑:添加了根据评论请求设置按钮的位置:

在您的 StoryBoard 中,确保您的按钮是 MyButton 类型(不再是 UIButton)。

然后在你用来设置标签的地方,不要使用标签,而是设置属性。所以替换这段代码:

cell.sharefb.tag = indexPath.row
cell.sharefb.addTarget(self, action: "showAlert:", forControlEvents:UIControlEvents.TouchUpInside)

与:

 cell.sharefb.row = indexPath.row
 cell.sharefb.section = indexPath.section
 cell.sharefb.addTarget(self, action: "showAlert:", forControlEvents:UIControlEvents.TouchUpInside)

【讨论】:

  • 我刚刚在你发帖的时候成功了,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-12
  • 1970-01-01
  • 2011-06-30
  • 1970-01-01
相关资源
最近更新 更多