当点击表格视图单元格中的标签时,将值传递给下一个视图

时间:2018-04-14 23:09:16

标签: ios swift uitableview

会喜欢传递值postArray [indexpath.row] .creatorId当tapview一个tableview单元格内的标签时,它可以传递到下一个视图控制器,这样我就可以加载该特定创建者/用户的配置文件。我使用了自定义单元格,因此如何根据所选标签(用户名)的位置获取创建者ID。

bind_param("ssi", $string, $string, $number);

1 个答案:

答案 0 :(得分:0)

以此为例 实现didSelectRow()方法并在其中编写如下内容:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // this method works, when you taped cell. write here code of you need. Next code only example, which set user info to some VC and push it:
        let controller =  UserController as? UserController
        if let controller = controller {
            controller.user = users[indexPath.row]
            self.navigationController?.pushViewController(controller, animated: true)
        }
    }   

将此添加到您的Cell类中:

func setTap() {
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapRecognized))
        self.label.addGestureRecognizer(tap)
        tap.numberOfTapsRequired = 1
    }

    @objc func tapRecognized(sender: UITapGestureRecognizer) {
        // here your code of tap on label
        print("label tapped")
    }    

检查storyBoard是你的标签isUserInteractionEnabled? - 将其设置为true。在tapRecodnized()方法里面做你需要的东西。你需要在你的单元格方法中调用方法setTap(),你在tableView中调用它(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - > UITableViewCell中。

更新

简单的例子。这段代码知道你点了什么。如果你点击单元格,但没有标签,添加推送一些控制器的代码,否则推动另一个控制器的代码 Cell's Class:

class MyTableViewCell: UITableViewCell {

    @IBOutlet weak var label: UILabel!

    var mainController: ViewController?

    func setText(text: String) {
        setTap()
        label.text = text
    }

    func setTap() {
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapRecognized))
        self.label.addGestureRecognizer(tap)
        tap.numberOfTapsRequired = 1
    }

    @objc func tapRecognized(sender: UITapGestureRecognizer) {
        if let mainController = mainController {
            print("label tapped")
            mainController.pushSomeVc(cell: self)
        }

    }
}   

主要类别代码:

class ViewController: UIViewController {

    @IBOutlet weak var myTableView: UITableView!

    var array = ["1", "2", "3", "4", "5", "6"]

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    func pushSomeVc(cell: MyTableViewCell) {
        let row = myTableView.indexPath(for: cell)?.row
        if let row = row {
            // write here code of push controller, when label tapped. row property for get some user from array
            print("push some vc with \(row)")
        }
    }

}

extension ViewController: UITableViewDelegate, UITableViewDataSource {

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = myTableView.dequeueReusableCell(withIdentifier: "cell") as? MyTableViewCell
        if let cell = cell {
            cell.setText(text: array[indexPath.row])
            cell.mainController = self
        }
        return cell ?? UITableViewCell()
    }


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
        // write here code of push controller with comments
        print("cell tapped: \(indexPath.row)")
    }

} 

我测试了这段代码,它的工作非常完美

相关问题