Ios 点击表格单元格时如何推送新的视图控制器?
我已经创建了这个包含3个部分和7行的表。代码如下所示Ios 点击表格单元格时如何推送新的视图控制器?,ios,swift,uitableview,navigationcontroller,Ios,Swift,Uitableview,Navigationcontroller,我已经创建了这个包含3个部分和7行的表。代码如下所示 import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet var subjectTabelView: UITableView! var slSubject = ["English Lang&Lit", "Chinese Lang&Lit", "Economics"]
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只有一个原型单元和一个单元标识符
谢谢大家! 要在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];
}
您可以使用prepareforsgue方法。您只需要设置目标视图。或者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.
}
}
}
假设您的“位置VC”是:
然后,只需将下面的函数添加到名为UIViewController
的ViewController中的代码中即可实现您的目标
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
s
1.-让我们创建一个类,该类将成为每个新的UIViewController
s(我们愿意从tableview单元格点击的对象)的父类:
2.-让我们创建一些导航规则,只是为了组织;-)
现在,让我们以前面的代码为基础:
3.-为了清楚起见,让我们稍微更改一下以前的LocationVC
(在本例中,将有一个序列图像板标识符,文本为“LocationVC_1”)
4.-现在,我们在didselectrowatinexpath
函数中触发所有这些
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)
}
}
}
请注意,使用协议和委托方法可以获得相同的结果,这只是简单的解释
这段代码在Objective-C中,问题是如何在中实现这一点swift@Hugo在他的问题中说他已经创建了一个导航控制器。@MSU_Bulldog Hi谢谢你的建议,但是如果我想让不同的单元格推送不同的视图,这意味着会有更多的多个标识符呢?谢谢我想用这个应用程序做的是,我想为所有科目设置一个不同的视图控制器,应用程序将允许用户输入每个科目的分数,应用程序将绘制输入的值。故事板的图片未显示。@HugoAlonso Hi抱歉stackOverflow不允许我发布图片,因为我的声誉不高enoughHi!我有一个问题,这会显示viewController中单元格的内容吗?例如,在twitter上,当您单击一条tweet时,它会在另一个viewController中打开该tweet,但只显示该tweet并允许您对其进行评论。@HugoAlonso,那么我是否要创建一个新文件LocationVCViewController.swift?此解决方案是否允许我为7个不同的单元格创建7个不同的视图控制器?感谢you@BrunoRecillas是的,你可以用这种方法做类似的事情,你只需传递你的推特、数据等的一个实例,而不仅仅是
.textToShow
中的文本,然后你就可以开始了。@ShawnClarke现在,提前知道这件事会很好,很抱歉没有早点抓到;-)(只是想让你知道,即使你不能发布图像,你也可以链接到它们)…所以,回答你的问题:仅仅创建LocationVCViewController
或LocationVC
是不够的。关于这一点,请向我澄清:您事先知道,对于要显示的每个新ViewController,哪一行(indexPath.row)将就位?换句话说,你的细胞总是在同一个地方。。例句:通向新VC_3的总是第3行,新VC_2总是第2行,等等。你能说得具体一点吗?求你了@雨果朗索
public class CommonDataViewController: UIViewController {
//Here we are going to be putting any data we want to share with this view
var data: AnyObject?
}
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)"
}
}
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
}
}
}
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)
}
}
}