我已经设置了一个tableView
,其中包含一个搜索栏和一个股票名称列表。我在我的Detail View Controller
中插入了一些代码,以便让单元格推送到它并在标签中说出一些内容。问题是每当我点击一个单元格时它会显示另一个单元格。是否有更好或更有效的方式推送到detail View
?
以下是代码:
import UIKit
class TableViewLemon: UITableViewController, UISearchResultsUpdating {
let tableData = ["Apple","Google","Facebook","Amazon","Exxon Mobil"]
var filteredTableData = [String]()
var resultSearchController = UISearchController()
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
// Reload the table
self.tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// 1
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// 2
if (self.resultSearchController.active) {
return self.filteredTableData.count
}
else {
return self.tableData.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
// 3
if (self.resultSearchController.active) {
cell.textLabel?.text = filteredTableData[indexPath.row]
return cell
}
else {
cell.textLabel?.text = tableData[indexPath.row]
return cell
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "DetailView") {
let VC = segue.destinationViewController as! DetailViewLemon
if let indexPath = self.tableView.indexPathForSelectedRow{
let Make = tableData[indexPath.row] as String
VC.sentData1 = Make
}
}
}
func updateSearchResultsForSearchController(searchController: UISearchController)
{
filteredTableData.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
let array = (tableData as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredTableData = array as! [String]
self.tableView.reloadData()
}
}
答案 0 :(得分:0)
我希望您的prepareForSegue
能够以filteredTableData
的方式区分tableData
和cellForRowAtIndexPath
。有点像:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "DetailView") {
let VC = segue.destinationViewController as! DetailViewLemon
if let indexPath = self.tableView.indexPathForSelectedRow {
let Make = self.resultSearchController.active ?
filteredTableData[indexPath.row] as String :
tableData[indexPath.row] as String
VC.sentData1 = Make
}
}
}
顺便说一句,我会避免使用segue.identifier
,因为它容易出错(即如果你弄错了没有编译错误)。相反,您可以在目标VC上使用if-let。
而不是:
if (segue.identifier == "DetailView") {
let VC = segue.destinationViewController as! DetailViewLemon
...
}
使用:
if let VC = segue.destinationViewController as? DetailViewLemon {
...
}