2016-11-28 45 views
0

我是Swift新手(我使用2.0),并没有任何编程经验。所以,我是新手)试图做一些相当简单的事情。链接一个UITableViewCell点击呼叫不同的ViewController。这就像用户点击单元车并打开新的ViewController链接一个单元格来调用不同的ViewController

我已经创建了ViewControllers (storyboard A,B,C,D,E)。 我如何连接?如果我正确理解我应该使用didSelectRowAtIndexPath。我曾问过

代码:

import UIKit 

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 


let list = ["Cars", "Fruits", "Foods", "Toys"] 
let listdesc = ["Toyota", "Apple", "Meat", "Fallout 4"] 
let identities = ["A", "B", "C", "D"] 

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cellIdentifier: String = "cell" 

    let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellIdentifier) 

    cell.textLabel?.text = list[indexPath.row] 
    cell.detailTextLabel?.text = listdesc[indexPath.row] 

    return cell 

} 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    } 

回答

0

你应该有这样做的didSelectRowForIndexPath:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

     let StoryBoard = UIStoryboard(name: "Main", bundle: nil) 

     if(list[indexPath.row] == "Cars") { 
     var vcA = STORYBOARD.instantiateViewControllerWithIdentifier("A") as! A 
     self.navigationController?.pushViewController(vcA, animated: true) 
     } else if(list[indexPath.row] == "Fruits") { 
      var vcB = STORYBOARD.instantiateViewControllerWithIdentifier("B") as! B 
      self.navigationController?.pushViewController(vcB, animated: true) 
     } 

    } 

和其他条件,你将管理如我....

+0

的,他应该设置'stoaryboard identifier'!你应该在回答中提到! – Lion

+0

是的..他必须设置标识符... –

+0

谢谢大家,我真的很感激。 as有2个严重错误! A和! B - >使用未声明的类型“A”和“B”。请检查我的故事板 - https://postimg.org/image/altu24893/ – JonnyA

0

由Gaurav回答,您可以检查didSelectRowAtIndexPath哪个indexPath被选中,然后导航到相应的控制器。

但是,如果您使用的是不同的故事板,那么self.storyboard而不是STORYBOARD可能会派上用场。

也从var更改您的viewController的对象,让你不改变它们,所以他们应该是常量。

而最后一个“总是避免力量解开”,而是自己处理错误。

0

你必须准备数据像这样和

options = [ 
     { 
     "category":"Cars" 
     "subcat":"Cars" 
     "viewType":"A" 
     }, 
    { 
     "category":"Fruits" 
     "subcat":"Apple" 
     "viewType":"B" 
     },{ 
     "category":"Foods" 
     "subcat":"Meat" 
     "viewType":"C" 
     } 
    ] 

这不完全粘贴复制的回答对你的东西,但你需要创建一个类似数据的数据。 您需要将数据全部修改到一起,以便使用单个字典对象中的特定索引,您将一次获得所有数据。如果你的数组没有正确的数据,那么你的应用程序可能会崩溃,如果索引将不适当。

然后,对于特定的索引,你可以得到viewType。你可以将它与需要的类型进行比较,并获取viewcontroller引用并推送它。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

     if(options[indexPath.row]["viewType"] == "A") { 
     // Get A controller reference and use it 
     }else if(options[indexPath.row]["viewType"] == "B") { 
     // Get A controller reference and use it 
     }else if(options[indexPath.row]["viewType"] == "C") { 
     // Get A controller reference and use it 
     } 
    } 

我希望你能理解结构。请让我知道你是否不清楚。

更新时间: 从故事板获得参考,

let storyboard = UIStoryboard(name: "StoryboardName", bundle: nil) 
let controller = storyboard.instantiateViewController(withIdentifier: "someViewController") as! UIViewController 
self.present(controller, animated: true, completion: nil) 
+0

谢谢,它非常清楚。但是,我如何才能“获取控制器参考并使用它”?我真的很新。 – JonnyA

+0

我已经添加了几行只是检查。 – Sandy

0

试试这个。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 


     // If storyboard name is A.storyboard. 
     let storyboard = UIStoryboard(name: "A", bundle: nil) 
     let vc = storyboard.instantiateViewControllerWithIdentifier("nextViewControllerId") as! nextViewController // The `nextViewController` storyboardId is supposed as "nextViewControllerId" 
     self.presentViewController(vc, animated: true) 
    } 

每次单击任何单元格时,都会显示nextViewController。如果你想让特定的单元格打开不同的viewcontroller,使用你在这个函数中使用条件的逻辑。

0

1.从TableViewController Scene创建一个segue到其他 ViewControllerScene。

2.给每个场景赋予唯一的标识符。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    //Based on your condition. 
    //Initiates the segue with the specified identifier from the current view controller's storyboard file. 
    performSegue(withIdentifier: "segueA", sender: self) 
} 
0

如果您有三个故事板,如Main,PreLogin和PostLogin。然后,你首先要声明的所有故事板是这样的: -

let mainStoryboard = UIStoryboard(name: "Main", bundle: nil) 
let preLoginStoryboard = UIStoryboard(name: "PreLogin", bundle: nil) 
let postLoginStoryboard = UIStoryboard(name: "PostLogin", bundle: nil) 

您从didSelectRowAtIndexPath方法方法一样,调用不同的viewController:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    switch indexPath.row{ 
     case 0: 
      let firstViewControllerScene = mainStoryboard.instantiateViewControllerWithIdentifier("firstViewControllerId") as! FirstViewController 
      self.presentViewController(firstViewControllerScene, animated: true) 

     case 1: 
      let secondViewControllerScene = preLoginStoryboard.instantiateViewControllerWithIdentifier("secondViewControllerId") as! SecondViewController 
      self.presentViewController(secondViewControllerScene, animated: true) 

     case 2: 
      let thirdViewControllerScene = postLoginStoryboard.instantiateViewControllerWithIdentifier("thirdViewControllerId") as! ThirdViewController 
      self.presentViewController(thirdViewControllerScene, animated: true) 

     default: 
      break 
     }  
} 
相关问题