简体   繁体   English

Xcode:TableView单元格创建ViewController

[英]Xcode: TableView cell to create ViewController

I've recently started using Xcode and Beginner to creating apps. 我最近开始使用Xcode和Beginner创建应用程序。 I've come across something I'd like to implement but research hasn't been quite clear/complicated to understand. 我遇到了一些我想实现的东西,但是研究还不是很清楚/理解起来很复杂。

I have a TableView with Days of Week and also a segmentedControl that duplicates these days 3 times. 我有一个带有星期几的TableView,还有一个segmentedControl,这几天重复了3次。 I'm wondering how I'm able to get user click to take me to an alternative ViewController depending on which day it is and what segment of the segmentedController is selected without having to create 21 viewcontrollers in the storyboard. 我想知道如何能够使用户单击以带我到另一个ViewController,这取决于它是在哪一天以及选择了segmentedController的哪个部分,而不必在情节提要中创建21个ViewController。

I've used a ViewController and made an outlet to a tableView for this setup. 我使用了ViewController并为此设置了tableView的出口。

具有segmentedController的TableView

You can have one viewcontroller which is basically a recipe for the viewcontroller you want to go to. 您可以拥有一个viewcontroller,这基本上是您要使用的viewcontroller的配方。

For example you want to show a viewcontroller with a label that states a combination of the number from the segmented control and the date which the user tapped on. 例如,您想显示一个带有标签的视图控制器,该标签说明分段控件中的数字和用户点击的日期的组合。

If you now create a ViewController which has a label in storyboard, the text on the label doesnt matter, you can use code to change the text to anything you would like. 如果现在创建一个在情节提要板上具有标签的ViewController,则标签上的文本无关紧要,您可以使用代码将文本更改为所需的任何内容。

In the tableviewcontroller you can use the didSelect delegate method of your tableview to present the viewcontroller and set the label to the values of the segmented control and the cell which is tapped. 在tableviewcontroller中,您可以使用tableview的didSelect委托方法来呈现ViewController,并将标签设置为分段控件的值和被点击的单元格。

I think you just need some more information about iOS and programming in general, therefore I suggest you watch the following iTunes U course: 我认为您一般只需要有关iOS和编程的更多信息,因此,我建议您观看以下iTunes U课程:
https://itunes.apple.com/us/course/developing-ios-10-apps-with-swift/id1198467120 https://itunes.apple.com/cn/course/developing-ios-10-apps-with-swift/id1198467120
It has everything you need to know 它拥有您需要了解的一切

in you tableview delegate method -> didSelectedRowAtIndexPath 在你的tableview委托方法-> didSelectedRowAtIndexPath

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

and inside this method you can use the indexPath to get the cell you've clicked 在此方法内,您可以使用indexPath获取您单击的单元格

let cell = tableView.cellForRowAt(indexPath)

now you can get the information in this cell 现在您可以在此单元格中获取信息

Create a detail view controller with two variables (Ex - dayName and selectedSegmentIndex). 创建带有两个变量(例如-dayName和selectedSegmentIndex)的详细信息视图控制器。 And in your current viewController "tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)" push detailsViewController with these details like this 并在您当前的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