Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将表视图动态单元格链接到特定视图控制器?_Ios_Swift_Uitableview_Uiviewcontroller - Fatal编程技术网

Ios 如何将表视图动态单元格链接到特定视图控制器?

Ios 如何将表视图动态单元格链接到特定视图控制器?,ios,swift,uitableview,uiviewcontroller,Ios,Swift,Uitableview,Uiviewcontroller,我是iOS 9和Swift 2的初学者。我有一个包含动态和分组内容的表视图,我希望将每个单元格链接到特定的视图控制器(而不是详细信息控制器)。我知道如何使用带有分段的静态单元格来实现这一点,但如何使用动态单元格呢?首先,我使用动态单元格是因为我计划添加大量数据,其次是因为我实现了一个搜索控件来快速找到所需的条目。另外,当用户从搜索结果中选择单元格时,视图之间的这种特定链接必须起作用。这是我的密码: import UIKit class TableViewController: UITableV

我是iOS 9和Swift 2的初学者。我有一个包含动态和分组内容的表视图,我希望将每个单元格链接到特定的视图控制器(而不是详细信息控制器)。我知道如何使用带有分段的静态单元格来实现这一点,但如何使用动态单元格呢?首先,我使用动态单元格是因为我计划添加大量数据,其次是因为我实现了一个搜索控件来快速找到所需的条目。另外,当用户从搜索结果中选择单元格时,视图之间的这种特定链接必须起作用。这是我的密码:

import UIKit

class TableViewController: UITableViewController, UISearchResultsUpdating {

// MARK: Properties

let section = ["Section 1", "Section"]
let names = [["Name 1", "Name 2", "Name 3"], ["Name 4", "Name 5", "Name 6", "Name 7", "Name 8"]]
var tableData = ["Name 1", "Name 2", "Name 3", "Name 4", "Name 5", "Name 6", "Name 7", "Name 8"] // Look for a way to convert directly from "names" array!
var filteredData:[String] = []
var resultSearchController:UISearchController!

override func viewDidLoad() {
    super.viewDidLoad()
    resultSearchController = UISearchController(searchResultsController: nil)
    resultSearchController.searchResultsUpdater = self
    resultSearchController.hidesNavigationBarDuringPresentation = false
    resultSearchController.dimsBackgroundDuringPresentation = false
    resultSearchController.searchBar.searchBarStyle = UISearchBarStyle.Prominent
    resultSearchController.searchBar.sizeToFit()
    self.tableView.contentOffset = CGPointMake(0,CGRectGetHeight(resultSearchController.searchBar.frame))
    self.tableView.tableHeaderView = resultSearchController.searchBar
}

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

// MARK: - Table view data source

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if resultSearchController.active {
        return nil
    } else {
        return self.section[section]
    }
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    if resultSearchController.active {
        return 1
    } else {
        return self.section.count
    }
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    if resultSearchController.active {
        return filteredData.count
    } else {
        return self.names[section].count
    }
}

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

    // Configure the cell...
    if resultSearchController.active {
        cell.textLabel?.text = filteredData[indexPath.row]
    } else {
        cell.textLabel?.text = self.names[indexPath.section][indexPath.row]
    }
    return cell
}

func updateSearchResultsForSearchController(searchController: UISearchController) {
    if searchController.searchBar.text?.characters.count > 0 {
        filteredData.removeAll(keepCapacity: false)
        let searchPredicate = NSPredicate(format: "SELF CONTAINS %@", searchController.searchBar.text!)
        let array = (tableData as NSArray).filteredArrayUsingPredicate(searchPredicate)
        filteredData = array as! [String]
        tableView.reloadData()
    }
    else {
        filteredData.removeAll(keepCapacity: false)
        filteredData = tableData
        tableView.reloadData()
    }
}
我到处都找不到解决办法。由于我没有太多的经验,请写一些细节和代码。谢谢

编辑:更具体:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

// Here is the code I need to link the selected cell to the
// right View Controller in my storyboard. And this must work
// when the user pick the cell in search results view too.
// For example, if the cell contains "Name 1", it navigates
// to "View Controller 1".

}

当触摸单元格时,可以使用以下构造运行自己的代码

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    // Your code here
}

您的代码可能
performSegue
,或者您喜欢的任何其他内容。

谢谢您的提示!以下是我写的:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {        let rowIndexPath = tableView.indexPathForSelectedRow!
    var nameString:String
    var segueString:String
    if resultSearchController.active {
        resultSearchController.active = false
        nameString = filteredData[rowIndexPath.row].lowercaseString
        segueString = "\(nameString)" + "View"
        self.performSegueWithIdentifier(segueString, sender: self)
    } else {
        nameString = self.names[rowIndexPath.section][rowIndexPath.row].lowercaseString
        segueString = "\(nameString)" + "View"
        self.performSegueWithIdentifier(segueString, sender: self)
    }
}

我创建了名为“nameXView”的segues。因此,例如,链接“名称1”和“视图控制器1”的序列是“名称1视图”。然后,我可以根据单元格内容选择正确的序列。它也适用于搜索结果

单击要链接的单元格,然后控制拖动到要显示的视图。然后选择您的选项例如:Show、Present Modally等

您可以尝试在单元格内容视图中添加容器视图,并链接到您想要的视图控制器但如果我有动态内容,我如何链接?
names
中的每个项目都必须与“我的故事板”中的右视图链接。如果我使用静态单元格,我可以使用一个显示序列来显示正确的视图。对于动态内容,我必须将每个项目与代码中的右视图相关联。我使用9.3.2模拟器在Xcode 7中运行了此代码,bulld失败。是的,我知道我必须使用
didSelectRowAtIndexPath
。我找不到的是里面导航到右视图控制器的代码。例如,“名称1”必须与“视图控制器1”链接,“名称2”必须与“视图控制器2”链接。。。对于
名称中的所有项目
。在静态单元格中,我使用显示序列将每个单元格与右视图链接。但是我如何处理动态的呢?