簡體   English   中英

當點擊自定義TableView單元格中的UITextField時,選擇到新視圖

[英]Segue to new view when UITextField within Custom TableView Cell tapped

我在自定義UITableViewCell中有一個UITextField。

當自定義單元格中的UITextField被點擊時,我需要選擇一個新的視圖控制器。

嘗試的解決方案

我嘗試使用在CustomTableViewCell類中執行segue

public func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    print("Text Field Tapped")
    return false 
}

但這不起作用,因為performSegue是一個ViewController函數。 然后我嘗試

func someAction() {
    performSegue(withIdentifier: "identifier", sender: self) 
}

在MyViewController類中,但是那也不起作用(不確定原因)。 這是我的兩個類的樣子:

MyViewController(持有UITableView)

import UIKit

class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:SearchTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! SearchTableViewCell

        return cell
    }
}

CustomTableViewCell

import UIKit

class CustomTableViewCell: UITableViewCell, UITextFieldDelegate {
    @IBOutlet weak var someTextField: UITextField!

    override func awakeFromNib() {
        super.awakeFromNib()

        self.someTextField.delegate = self
    }
}

非常感謝您的聰明人可以提供的任何幫助:)

這樣的事情應該工作:

class TableViewController: UITableViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.estimatedRowHeight = 44
        tableView.rowHeight = UITableViewAutomaticDimension
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TFCell", for: indexPath) as! TFCell
        cell.textField.delegate = self
        return cell
    }
}

extension TableViewController: UITextFieldDelegate {
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        performSegue(withIdentifier: "TFSegue", sender: textField)
        return false
    }
}

class TFCell: UITableViewCell {
    @IBOutlet weak var textField: UITextField!
}

“ TFSegue”是從“ TableViewController”到情節提要中創建的targetviewcontroller的segue。 隨時詢問是否不清楚!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM