【问题标题】:IOS swift how can I get label text inside a TableView on label tapIOS swift如何在标签点击时在TableView中获取标签文本
【发布时间】: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


    【解决方案1】:

    你可以做这样的事情:

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

    【讨论】:

    • 这实际上非常接近它,上面代码的唯一问题是它总是插入第一列的值。我正在使用你的代码,并会尝试让它动态
    【解决方案2】:

    您不必使用UITapGestureRecognizer。只需使用委托方法。将UITableView 委托设置为您的UIViewController,并使该类符合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])
    }
    

    【讨论】:

    • 这行得通,只是我试图将它放在一个函数中,因为事情会变得很复杂。
    • 你总是可以从didSelect内部调用函数并传递适当的信息。
    • 谢谢你,这正是我们所需要的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多