我是 iOS 9 和 Swift 2 的初学者。我有一个包含动态和分组内容的表格视图,我想将每个单元格与特定的视图控制器(不是详细控制器)链接。我知道如何使用带有segues的静态单元格来完成此操作,但我如何处理动态单元格呢?拳头,我使用动态单元格,因为我计划添加大量数据,其次是因为我实现了搜索控件以快速找到所需的条目。此外,当用户从搜索结果中选择单元格时,视图之间的此特定链接必须起作用。这是我的代码:
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".
}
答案 0 :(得分:1)
您可以使用以下构造在触摸单元格时运行您自己的代码。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Your code here
}
您的代码可能是performSegue
,或者您喜欢的任何其他内容。
答案 1 :(得分:0)
感谢您的提示!这是我写的:
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。因此,例如,segue链接"名称1"和"查看控制器1"是" name1View"。然后,我可以根据细胞内容选择正确的segue。它也适用于搜索结果!
答案 2 :(得分:0)
单击要链接的单元格,然后控制拖动到要显示的视图。然后选择您的选项Ex:Show,Present Modally等