繁体   English   中英

Xcode:TableView单元格创建ViewController

[英]Xcode: TableView cell to create ViewController

我最近开始使用Xcode和Beginner创建应用程序。 我遇到了一些我想实现的东西,但是研究还不是很清楚/理解起来很复杂。

我有一个带有星期几的TableView,还有一个segmentedControl,这几天重复了3次。 我想知道如何能够使用户单击以带我到另一个ViewController,这取决于它是在哪一天以及选择了segmentedController的哪个部分,而不必在情节提要中创建21个ViewController。

我使用了ViewController并为此设置了tableView的出口。

具有segmentedController的TableView

您可以拥有一个viewcontroller,这基本上是您要使用的viewcontroller的配方。

例如,您想显示一个带有标签的视图控制器,该标签说明分段控件中的数字和用户点击的日期的组合。

如果现在创建一个在情节提要板上具有标签的ViewController,则标签上的文本无关紧要,您可以使用代码将文本更改为所需的任何内容。

在tableviewcontroller中,您可以使用tableview的didSelect委托方法来呈现ViewController,并将标签设置为分段控件的值和被点击的单元格。

我认为您一般只需要有关iOS和编程的更多信息,因此,我建议您观看以下iTunes U课程:
https://itunes.apple.com/cn/course/developing-ios-10-apps-with-swift/id1198467120
它拥有您需要了解的一切

在你的tableview委托方法-> didSelectedRowAtIndexPath

you can simply write 
let vc = UIViewController()
/*
you can add informations here 
*/
present(vc, animated: true)

在此方法内,您可以使用indexPath获取您单击的单元格

let cell = tableView.cellForRowAt(indexPath)

现在您可以在此单元格中获取信息

创建带有两个变量(例如-dayName和selectedSegmentIndex)的详细信息视图控制器。 并在您当前的viewController“ tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath)”中将detailsViewController与这些详细信息一起推送

let objViewController: DetailsViewController? = UIStoryboard.mainStoryboard().instantiateViewController(withIdentifier: "DetailsViewController") as? DetailsViewController
    objViewController?.dayName = dayName
    objViewController?.selectedSegment = segmentControl.selectedSegmentIndex
    navigationController?.pushViewController(objViewController ?? UIViewController(), animated: true)

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM