【发布时间】:2016-11-19 07:53:55
【问题描述】:
我有一个通过标签在其中包含数据的 TableView。当您点击标签时,Tap 会注册,但现在 我想获取点击标签的数据,但我很难完成。例如,我对 Buttons 具有相同的功能,这就是我对 TableView 中的按钮执行此操作的方式。
按钮点击事件
var locations = [String]()
@IBOutlet weak var Location: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
TableSource.dataSource = self
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Registration_Search", for: indexPath)
cell.Location.setTitle(locations[indexPath.row], for: UIControlState.normal)
cell.Location.addTarget(self, action: #selector(Registration_SearchController.Location_Click(sender:)), for: .touchUpInside)
cell.Location.tag = indexPath.row
return cell
}
func Location_Click(sender: UIButton) {
print(locations[sender.tag])
}
上面的代码允许我获取任何被点击的按钮的数据。我现在尝试对 Label 执行相同操作,但无法获取 Label 拥有的数据。这是我的标签代码,哦,和上面一样,但是不同的 ViewController
var locations = [String]()
@IBOutlet weak var location: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
TableSource.dataSource = self
location.isUserInteractionEnabled = true
}
func tapFunctionn(sender: UITapGestureRecognizer)
{
// I would like to get the data for the tapped label here
print("Tapped")
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Registration_Search", for: indexPath)
cell.location.text = Locations[indexPath.row]
let tap = UITapGestureRecognizer(target:self, action: #selector(HomePageC.tapFunctionn))
cell.location.addGestureRecognizer(tap)
return cell
}
当我再次单击标签时,它会打印 Tapped 但无法获取实际数据。在 Button 函数中,我可以使用 Sender.Tag 但 UITapGestureRecognizer 没有 Tag 方法。任何建议将不胜感激
【问题讨论】:
标签: ios swift uitableview swift3 uigesturerecognizer