搜索栏仅显示第一个Cell in detail视图

时间:2015-08-27 21:21:07

标签: swift

我在Swift中创建了一个带有搜索栏的TableView。 我创建了3个可搜索的对象。 (131,132,137) 当我搜索137并按137(这是唯一按下的选项)时,我仍然可以访问131 detailView。谁知道为什么?感谢

这是我的代码:

import UIKit

class TableViewController: UITableViewController, UISearchResultsUpdating {

let TitleList = ["131","132","137"]

override func viewDidLoad() {
    super.viewDidLoad()

    self.resultSearchController = UISearchController(searchResultsController: nil)
    self.resultSearchController.searchResultsUpdater = self
    self.resultSearchController.dimsBackgroundDuringPresentation = false
    self.resultSearchController.searchBar.sizeToFit()
    self.tableView.tableHeaderView = self.resultSearchController.searchBar
    self.tableView.reloadData()

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell: TableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! TableViewCell
    cell.LabelTitle.text = TitleList[indexPath.row]
    cell.CellDescription.text = DescriptionList[indexPath.row]


    let imagename = UIImage(named: ImageList[indexPath.row])
    cell.CellImage.image = imagename


    if self.resultSearchController.active {
        cell.LabelTitle?.text = self.filteredfarger[indexPath.row]
    }
    else {
        cell.LabelTitle?.text = self.TitleList[indexPath.row]
    }

    return cell


}

func updateSearchResultsForSearchController(searchController: UISearchController) {

    self.filteredfarger.removeAll(keepCapacity: false)
    let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
    let array = (self.TitleList as NSArray).filteredArrayUsingPredicate(searchPredicate)
    self.filteredfarger = array as! [String]
    self.tableView.reloadData()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if (segue.identifier == "DetailView") {
        let VC = segue.destinationViewController as! DetailedViewController
        if let indexpath = self.tableView.indexPathForSelectedRow {
            let Title = TitleList[indexpath.row] as String
            VC.SentData1 = Title
        }

1 个答案:

答案 0 :(得分:0)

因为当您进行搜索时,137的行为0,然后在prepareForSegue中使用tableView.indexPathForSelectedRow(表示indexPath.row == 0)来获取标题数据。尝试执行tableView.cellForRowAtIndexPath功能,检查resultSearchController是否有效,以及是否使用filteredfarger代替TitleList。这样的事情可能是:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if (segue.identifier == "DetailView") {
        let VC = segue.destinationViewController as! DetailedViewController
        if let indexpath = self.tableView.indexPathForSelectedRow {
            if self.resultSearchController.active {
                let Title = self.filteredfarger[indexpath.row]
                VC.SentData1 = Title
            }
            else {
                let Title = TitleList[indexpath.row] as String
                VC.SentData1 = Title
            }

        }
    }
}

更好的是,你已经给了细胞一些信息,为什么不使用它,有点像这样:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if (segue.identifier == "DetailView") {
        let VC = segue.destinationViewController as! DetailedViewController
        if let cell = sender as? TableViewCell {
            VC.SentData1 = cell.LabelTitle.text
        }
    }
}