首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >以编程方式将UITableViewCell标签链接到下一个ViewController

以编程方式将UITableViewCell标签链接到下一个ViewController
EN

Stack Overflow用户
提问于 2016-12-19 10:04:18
回答 2查看 682关注 0票数 0

我以编程方式在一个TableViewControllers中创建了2个ViewController,如下所示:

代码语言:javascript
运行
复制
// contents for labels in cells for tableviewcontrollers

let contents1 : [String] = ["One:","Two:","Three:","Four:","Five:"]

let contents2 : [String] = ["Six:","Seven:","Eight:","Nine:","Ten:"]

override func viewDidLoad() {
    super.viewDidLoad()

    table1.delegate = self
    table1.dataSource = self

    table2.delegate = self
    table2.dataSource = self   
}

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

    if(tableView.tag == 1)
    {
        return contents1.count
    }
    else if (tableView.tag == 2)
    {
        return contents2.count
}
    return 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

  let cell = tableView.dequeueReusableCell(withIdentifier: "cell",  for: indexPath as IndexPath )

    if (tableView.tag == 1)
    {
        cell.textLabel?.text = contents1[indexPath.row]
    }
    else if (tableView.tag == 2)
    {
        cell.textLabel?.text = contents2[indexPath.row]

    }
return cell
}

我的问题是,如果选择在不使用Segue的情况下显示下一个新的TableViewController,如何以编程方式链接第一个ViewController的标签“4:”?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-12-19 10:44:39

您可以使用UITableView's deSelectRowAt委托方法来标识单元格选择,然后再转到下一个视图。

代码语言:javascript
运行
复制
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)

    if cell?.textLabel?.text == "Four" { // you can use you indexPath to compare as well
        let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController
        self.navigationController.pushViewController(secondViewController, animated: true)
    }
}
票数 0
EN

Stack Overflow用户

发布于 2017-10-24 07:20:36

代码语言:javascript
运行
复制
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = pets[indexPath.row]
    // Configure the cell...

    return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    myIndex = indexPath.row
    performSegue(withIdentifier: "segue", sender: self)
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41220059

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档