简体   繁体   中英

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

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

You can make something like that:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM