IOS swift如何在标签点击时在TableView中获取标签文本 [英] IOS swift how can I get label text inside a TableView on label tap

查看:34
本文介绍了IOS swift如何在标签点击时在TableView中获取标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过标签在其中包含数据的 TableView.当您点击标签时,Tap 会注册,但现在我想获取点击标签的数据,但我很难完成这项工作.例如,我对 Buttons 有相同的功能,这就是我对 TableView 中的按钮执行此操作的方式.

I have a TableView that has data inside of it via Labels. When you click the label the Tap registers but now I would like to get the Data of the clicked label and I am having a difficult time getting that done. I have the same functionality working for Buttons for instance this is how I do it for my buttons inside a 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

That code above allows me to get the Data of any Button that is clicked . I now try to do the same for the Label but can't get the data that the Label has . This is my Code for the Label and oh the same are the same as above but different 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 方法.任何建议将不胜感激

Again when I click the label it prints Tapped but can't get the actual data. In the Button function I could use Sender.Tag but the UITapGestureRecognizer does not have a Tag method . Any suggestions would be greatly appreciated

推荐答案

您不必使用 UITapGestureRecognizer.只需使用委托方法.将 UITableView 委托设置为您的 UIViewController 并使类符合 UITableViewDelegate

You don't have to use UITapGestureRecognizer. Just use the delegate method. Set the UITableView delegate to your UIViewController and make the class conform to the UITableViewDelegate

对于 swift 3

override func viewDidLoad() {
    super.viewDidLoad()
    TableSource.dataSource = self
    TableSource.delegate = self
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)

    //access the label inside the cell
    print(cell.label?.text)
    //or you can access the array object
    //print(Locations[indexPath.row])
}

这篇关于IOS swift如何在标签点击时在TableView中获取标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆