IOS swift how can I get label text inside a TableView on label tap

十年热恋 提交于 2019-12-12 04:24:00

问题


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 .

Button Click Event

      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])


    }

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
    }

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


回答1:


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

For 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])
}



回答2:


You can make something like that:

func tapFunctionn(recognizer: UIPinchGestureRecognizer) {
  let view = recognizer.view
  let index = view?.tag
  print(index)
}


来源:https://stackoverflow.com/questions/40690518/ios-swift-how-can-i-get-label-text-inside-a-tableview-on-label-tap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!