ios - 如何使用 TableView 项目访问 Swift 2 中的 View Controller ?

标签 ios swift uitableview swift2

我正在尝试创建我计划发布到 App Store 的第一个 iOS 应用程序。它是一个选项卡式应用程序,每个 View Controller 中都嵌入了 TableView。这是我的一个选项卡式 View Controller 的代码:

import Foundation
import UIKit

class AU: UITableViewController {

    var textArray = ["001", "002", "003", "004", "005", "006", "007", "008", "009", "010", "011", "012", "013", "014", "015", "016", "017", "018", "019", "020", "021", "022", "023", "024", "025", "026", "027", "028", "029", "030", "031", "032", "033", "034", "035", "036", "037", "038", "039", "040", "041", "042" ]

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

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("AUCell") as UITableViewCell!

        cell.textLabel?.text = textArray[indexPath.row]

        return cell
    }

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

所以,基本上,当用户点击 textArray 中的一项时,我想切换到另一个 View Controller 。我目前正在使用 Swift 2 编写此内容。

最佳答案

UITabBarController 中嵌入的每个 UIViewController 都有一个对 UITabBarController 及其 tabBarController 属性的引用。要更改标签栏的选定 View Controller ,请设置标签栏的 selectedViewController:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if (indexPath.row == 0) { // check the row that was selected
        self.tabBarController?.selectedIndex = 0 // select a new tab on the tab bar, changing the selected view controller
    }
}

关于ios - 如何使用 TableView 项目访问 Swift 2 中的 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36119186/

相关文章:

iphone - 什么是 key 以及如何使用它们?

ios - 仅将触摸事件转发给被触摸的 View

ios - 以前的 UITableView 中的项目出现在新的 UITableView 中

ios - 在 Cocos2d 环境中运行计时器的准确性

swift - 如何通过首先检查其类型来修改 'Any' 变量?

ios - 我可以在点击状态栏时禁用/编辑自动跳转到顶部滚动吗?

ios - 创建几个按钮

ios - 如何在 Tableview 部分标题中添加两个 UIButton

arrays - 使用 Array.map 在字典数组中搜索

ios - 尝试在 iOS 中动态调整 UITableView 的大小时出现运行时异常