简体   繁体   English

如何在Swift 2.0中使用多个View Controller创建动态Table View单元格?

[英]How to create Dynamic Table View cells With multiple View Controller in swift 2.0?

Hi all. 大家好。 I'm troubling with tableView in swift. 我很快就麻烦了tableView。 Actually i created the table view with two rows (About and Login) in main view controller. 实际上,我在主视图控制器中创建了具有两行 (关于和登录)的表视图。 Problem at initial, when i click the about or Login , then the New View controller is not opened. 最初出现问题,当我单击about或Login时, 未打开 New View控制器 But, I try second time to another one row, then the first clicked view Controller is opened at this time of Clicking. 但是,我第二次尝试到另一行,然后在单击时打开第一个单击的视图控制器。 so, this cycles shown wrong view controller at every time of clicking. 因此,此循环在每次单击时显示错误的视图控制器。 Please tell me as whats wrong with my Code?? 请告诉我我的代码有什么问题吗? Please refer screenshot given below.Thanks in advance!!! 请参考下面的截图 。谢谢!!!

import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {


    @IBOutlet var tableView: UITableView!

    var titles = ["About","LogIn"]
    override func viewDidLoad() {
        super.viewDidLoad()

    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath) as! TableCell
        cell.label.text = titles[indexPath.row]
        return cell
    }

    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
        switch indexPath.row
        {
        case 0: self.performSegueWithIdentifier("aboutSegue", sender: self)
        case 1: self.performSegueWithIdentifier("loginSegue", sender: self)
        default: break
        }
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "aboutSegue"
        {
          let vc = segue.destinationViewController as! about
            vc.title = "About"
        }
        else
        {
            let vc = segue.destinationViewController as! login
            vc.title = "Login"

        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }


}

Below Screen shown, I clicked About Row. 在显示的屏幕下方,我单击了关于行。 But, the Login ViewController is Opened. 但是,登录ViewController已打开。

在此处输入图片说明

This is my Story Board Connections. 这是我的故事板连接。 在此处输入图片说明

Just update didSelectRowAtIndexPath method 只需更新didSelectRowAtIndexPath方法

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    switch indexPath.row
    {
    case 0: 
        self.performSegueWithIdentifier("aboutSegue", sender: self) 
        break;
    case 1: 
        self.performSegueWithIdentifier("loginSegue", sender: self)
        break;

    default: break
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将多个选定的表视图单元格数据传输到Swift中的下一个视图控制器? - How do I transfer multiple selected table view cells data to the next view controller in Swift? 如何用内容填充分组的动态Table View单元格(快速) - How to fill grouped dynamic Table View cells with content (swift) 如何快速订购两个动态表格视图单元格? - How can I order two dynamic table view cells in swift? 如何使用来自另一个视图控制器的用户输入来使用表视图单元格创建列表 - How to make a list with Table View Cells with user input from another view controller swift 如何在表格视图单元格中创建动态集合视图,而不快速重新加载表格视图? - How to create dynamic collection view in a table view cell, without reloading table view swift? 如何在Swift中从另一个视图访问视图的表格视图的单元格? - How to access cells of a table view of a view from another view in Swift? 如何在表视图Swift 2.0中对A-z进行排序 - How to sort A - z in table view swift 2.0 如何根据用户输入在表格视图 swift 中创建动态部分? - How to create dynamic section in table view swift based on the user input? UIViewController中的多个子表视图控制器Swift - Multiple child table view controller in UIViewController swift Swift 2.0在一个视图控制器cellForRowIndexPath中包含两个表视图 - Swift 2.0 two table views in one view controller cellForRowIndexPath
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM