2015-08-14 50 views
1

我已经创建了3个部分和7行这个表。代码如下所示如何在单击表格单元时推送新的视图控制器?

import UIKit 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

@IBOutlet var subjectTabelView: UITableView! 

var slSubject = ["English Lang&Lit", "Chinese Lang&Lit", "Economics"] 
var hlSubject = ["Mathematics", "Chemistry", "Biology"] 
var tokSubject = ["Theory of Knowledge"] 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    subjectTabelView.dataSource = self 
    subjectTabelView.delegate = self 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if section == 0{ 
     return hlSubject.count 
    }else if section == 1{ 
     return slSubject.count 
    }else { 
     return tokSubject.count 
    } 

} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let subjectCell = tableView.dequeueReusableCellWithIdentifier("idSubjectCell", forIndexPath: indexPath) as! UITableViewCell 

    if indexPath.section == 0 { 
     subjectCell.textLabel?.text = hlSubject[indexPath.row] 
    } else if indexPath.section == 1{ 
     subjectCell.textLabel?.text = slSubject[indexPath.row] 
    } else { 
     subjectCell.textLabel?.text = tokSubject[indexPath.row] 
    } 

    return subjectCell 
} 

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    if section == 0 { 
     return "HL" 
    } else if section == 1{ 
     return "SL" 
    } else { 
     return "ToK" 
    } 
} 



} 

我有什么做的,使在这个表中的每个单元格将一个新视图控制器,当它被窃听?我的故事板的图片如下所示。在我的故事板中,我的视图控制器,我已经创建了一个导航控制器,并使视图控制器具有rootViewController表。而现在,我的tableView只有一个原型单元格和一个单元格标识符。

谢谢!

+1

你的故事板的图片不显示。 –

+0

@HugoAlonso嗨抱歉stackoverflow不会让我张贴照片因为我的名誉不够高 –

回答

0

那么在一个UINavigationController推视图控制器,你只需使用此代码:

ViewController *viewController = [self.navigationController.storyboard instantiateViewControllerWithIdentifier:@"locationVC"]; 
[self.navigationController pushViewController:viewController animated:YES]; 

你正在寻找的方法是这样的一个:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    ViewController *viewController = [self.navigationController.storyboard instantiateViewControllerWithIdentifier:@"locationVC"]; 
    [self.navigationController pushViewController:viewController animated:YES]; 
} 
+0

此代码是在Objective-C中,问题是关于如何在swift中实现这个问题 –

+0

@Hugo它在他的问题中说他已经创建一个导航控制器。 – dmarsi

+0

是的,你是对的,我编辑我的评论 –

0

你可以使用prepareForSegue方法。你只需要设置目标视图。或didSelectRowAtIndexPath方法 prepareForSegue代码如下所示:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
if segue.identifier == "nameofTheSegue" 
{ 
    if let destinationVC = segue.destinationViewController as? OtherViewController{ 
     // do whatever you want with the data you want to pass. 
    } 
} 
} 
1

假设你的 “locationVC” 是:

class LocationVC: UIViewController { 

    @IBOutlet weak var fromWhereLabel: UILabel! 
    //This can be changed when creating this UIViewController 
    var textToShow : String? 

    override func viewWillAppear(animated: Bool) { 
     if let textToShow = textToShow { 
      fromWhereLabel.text = textToShow 
     } 
    } 
} 

然后,只需添加以下代码功能的ViewController命名UIViewController应该有更好的名字;-))你可以达到你的目标。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     //if such cell exists and destination controller (the one to show) exists too.. 
     if let subjectCell = tableView.cellForRowAtIndexPath(indexPath), let destinationViewController = navigationController?.storyboard?.instantiateViewControllerWithIdentifier("locationVC") as? LocationVC{ 
      //This is a bonus, I will be showing at destionation controller the same text of the cell from where it comes... 
      if let text = subjectCell.textLabel?.text { 
       destinationViewController.textToShow = text 
      } else { 
       destinationViewController.textToShow = "Tapped Cell's textLabel is empty" 
      } 
      //Then just push the controller into the view hierarchy 
      navigationController?.pushViewController(destinationViewController, animated: true) 
     } 
    } 

你将能够有一个LocationVCUIViewController发布的每一个你点击一个细胞时,它就会有一定的价值,证明是正确的。 :)

希望它有助于!

更新:下面代码和指令是允许在细胞后自来水推出 不同UIViewController小号

1 .-让我们创建一个类,将是每一个家长我们新的UIViewController s(我们愿意从我们的台式电池的水龙头去的那些):

public class CommonDataViewController: UIViewController { 
//Here we are going to be putting any data we want to share with this view 
    var data: AnyObject? 
} 

2:让我们建立某种形式的导航规则,只是将举办;-)

enum Navigation: Int { 
    case vc1 = 0, vc2 = 1, vc3 = 2, vc4 = 3 
    //How many rules we have (for not to exceed this number) 
    static let definedNavigations = 4 
    //This must return the identifier for this view on the Storyboard 
    func storyboardIdentifier() -> String { 
     //for this example's sake, we have a common prefix for every new view controller, if it's not the case, you can use a switch(self) here 
     return "locationVC_\(self.rawValue + 1)" 
    } 
} 

现在,让我们建立在以前的代码:

3。 - 为清楚起见,让我们改变一点我们以前LocationVC(即在这个例子中,将有一个故事板标识符与文本“locationVC_1”

class LocationVC: CommonDataViewController { 

    @IBOutlet weak var fromWhereLabel: UILabel! 

    //This is optional, but improves clarity..here we take our AnyObject? variable data and transforms it into the type of data this view is excepting 
    var thisVCReceivedData: String? { 
     return data as? String 
    } 

    override func viewWillAppear(animated: Bool) { 
     if let textToShow = thisVCReceivedData { 
      fromWhereLabel.text = textToShow 
     } 
    } 
} 

4.-现在,我们触发了这一切在我们的didSelectRowAtIndexPath功能。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    //Just to avoid tapping on a cell that doesn't have an UIViewController asociated 
    if Navigation.definedNavigations > indexPath.row { 
     //The view's instance on our Navigation enum to which we most go after tapping this cell 
     let nextView = Navigation(rawValue: indexPath.row)! 
     //The identifier of the destination CommonDataViewController's son in our Storyboard 
     let identifier = nextView.storyboardIdentifier() 
     //If everything exists... 
     if let subjectCell = tableView.cellForRowAtIndexPath(indexPath), let destinationViewController = navigationController?.storyboard?.instantiateViewControllerWithIdentifier(identifier) as? CommonDataViewController { 

      //here you can use a switch around "nextView" for passing different data to every View Controller..for this example, we just pass same String to everyone 
      if let text = subjectCell.textLabel?.text { 
       destinationViewController.data = text 
      } else { 
       destinationViewController.data = "Tapped Cell's textLabel is empty" 
      } 
      navigationController?.pushViewController(destinationViewController, animated: true) 
     } 
    } 
} 

注意,您可以使用实现相同的协议结果和委托的方式,这只是简单的解释

+0

嗨!我有一个问题,这会显示viewController中单元格的内容吗?例如,在Twitter上,当您点击一条推文时,它会在不同的viewController中打开推文,但只显示推文并允许您对推文发表评论。 –

+0

@HugoAlonso,所以我创建一个新文件LocationVCViewController.swift?这个解决方案能让我为7个不同的单元创建7个不同的视图控制器吗?谢谢 –

+0

@BrunoRecillas是的,你可以用这种方法做类似的事情,你只需传递你的推文,数据等的实例,而不是'.textToShow'中的文本,你就可以开始了。 –

相关问题