self.performSegue(withIdentifier:“teacherDetail”,sender:self)需要两次点击

时间:2017-08-28 23:45:52

标签: ios swift uitableview uisearchcontroller

我试图从swift 3中的tableview控制器中的搜索结果中删除,但是当我使用self.performSegue(withIdentifier: "teacherDetail", sender: self)时,它似乎在选择单元格时挂起。如果我点击第一个单元格,它将显示为选中的灰色,然后除非我选择另一个单元格,否则将不会执行任何操作。然后,它将使用来自第一个单元格的信息将segue预先形成到详细视图控制器。

import UIKit

var name = ""

class DirectoryTC: UITableViewController, UISearchResultsUpdating {
    var teachers = ["Mr. Delano", "Mr. Antani", "Mr. Botelho", "Mr. Braga"]
    var filteredTeachers = [String]()

    var searchController: UISearchController!
    var resultsController = UITableViewController()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.resultsController.tableView.dataSource = self
        self.resultsController.tableView.delegate = self

        self.searchController = UISearchController(searchResultsController: self.resultsController)
        self.tableView.tableHeaderView = self.searchController.searchBar
        self.searchController.searchResultsUpdater = self
        definesPresentationContext = true
    }

    func updateSearchResults(for searchController: UISearchController) {
        self.filteredTeachers = self.teachers.filter{ (teacher:String) -> Bool in
            if teacher.lowercased().contains(self.searchController.searchBar.text!.lowercased())
            {
                return true
            }else
            {
                return false
            }
        }

        self.resultsController.tableView.reloadData()
    }

    // MARK: - Table view data source

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if tableView == self.tableView
        {
            return self.teachers.count
        }else
        {
            return self.filteredTeachers.count
        }

    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = UITableViewCell()

        if tableView == self.tableView
        {
            cell.textLabel?.text = self.teachers[indexPath.row]
        }else
        {
            cell.textLabel?.text = self.filteredTeachers[indexPath.row]
        }

        return cell
    }

    override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {

        DispatchQueue.main.async(){

            if tableView == self.tableView
            {
                name = self.teachers[indexPath.row]
            }else
            {
                name = self.filteredTeachers[indexPath.row]
            }

            self.performSegue(withIdentifier: "teacherDetail", sender: self)
        }

    }
}

这是我想要查看的视图控制器的swift文件。

import UIKit

class DirectoryDetailVC: UIViewController{
    @IBOutlet weak var test: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        test.text = name
    }
}

2 个答案:

答案 0 :(得分:0)

确保你的故事板视图控制器名称是teacherDetail - 使用

DispatchQueue.main.async { self.performSegue(withIdentifier: "teacherDetail", sender: self)}

而不是

self.performSegue(withIdentifier: "teacherDetail", sender: self)

答案 1 :(得分:0)

rmaddy抓住了它。这是一个错字。 “错字 - 将>>> from scrapy.selector import Selector >>> selector = Selector(text=page) >>> selector.xpath('.//table[1]/tr[2]/td[5]/text()').extract()[0] '41-23' 更改为didDeselectRowAtlectRowAt