ios - prepareForSegue 将从数组中提取的错误(延迟)字符串发送到第二个 View Controller

标签 ios swift xcode7

在 VC1 中,我尝试从数组中提取字符串,并将其发送到 VC2。我在prepareForSegue 方法中执行此操作。但是,当我运行它时,出现了延迟:

  1. 我在 VC1 中点击 row1(苹果),它会转到 VC2 并在 VC2 中显示苹果
  2. 点击返回 VC 1
  3. 点击 row2(bananas),字符串作为苹果传递到 VC2
  4. 点击返回 VC1
  5. 我在 VC1 中点击 row3(橙子),它会转到 VC2 并在 VC2 中显示香蕉 等等等等

下面是我正在使用的代码。如果有人能帮助我告诉我哪里出了问题,我将不胜感激。谢谢..

class HomeSearchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating, UISearchBarDelegate {

var filteredArray = [String]()
var shouldShowSearchResults = false
var searchController: UISearchController!

var databaseRef = FIRDatabase.database().reference()
var loggedInUser = AnyObject?()
var loggedInUserData = AnyObject?()


var productSearchArray = [AnyObject?]()
var productBrandArray = [String]()
var itemNameArray = [String]()
var picUrlArray = [String]()
var skuArray = [String]()
var nextScreenRow = Int()
var nextSceenRowNonSearch = Int()
var toPass = String()
var toPassSearch = String()
var initialSearchList = ["Apple", "Samsung", "Windows", "Google"]

    @IBOutlet weak var tblSearchResults: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()


        loadListOfCountries()
        configureSearchController()


        tblSearchResults.delegate = self
        tblSearchResults.dataSource = self
    }

// MARK: Table Cells Stuff

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }


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

        if shouldShowSearchResults {
            return filteredArray.count
        }
        else {
            return initialSearchList.count
        }
    }


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("homeSearchCell", forIndexPath: indexPath) as! HomeSearchTableViewCell

      //note to self: need to pass on more than one variable for search

        if shouldShowSearchResults {

            cell.configure(filteredArray[indexPath.row], itemNameLabel: "text'", skuLabel: "TEXT")
            //cell.textLabel?.text = filteredArray[indexPath.row]
        }
        else if shouldShowSearchResults == false{
            cell.configureNISearch(initialSearchList[indexPath.row])

        }

        return cell
    }


    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 60.0
    }

//找出行并传递变量。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)


    //println("You selected cell #\(indexPath.row)!")spDetailSegue
    if shouldShowSearchResults {
        nextScreenRow = indexPath.row

        // get to the next screen
        self.performSegueWithIdentifier("spDetailSegue", sender: self)

    }
    else if shouldShowSearchResults == false{

        nextSceenRowNonSearch = indexPath.row

        // get to the next screen
        self.performSegueWithIdentifier("searchSegue", sender: self)
    }

}

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



    if (segue.identifier == "spDetailSegue") {
        //Checking identifier is crucial as there might be multiple
        // segues attached to same view
        let detailVC = segue.destinationViewController as? pDetailViewController;
        detailVC!.toPassSku = skuArray[nextScreenRow]

        // print("vSku", )
    }
    if (segue.identifier == "searchSegue"){

        //print("selected indexpath = ", selectedIndex)
        let detailVC = segue.destinationViewController as? SearchDetailViewController;
        detailVC!.toPassSearch = initialSearchList[nextSceenRowNonSearch]
    }

}

//搜索栏内容

func configureSearchController() {
        // Initialize and perform a minimum configuration to the search controller.

        searchController = UISearchController(searchResultsController: nil)
        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        searchController.searchBar.placeholder = "Search here..."
        searchController.searchBar.delegate = self
        searchController.searchBar.sizeToFit()
        //searchController.searchBar.scopeButtonTitles = ["Products", "Users"]
        searchController.searchBar.barTintColor = UIColor.whiteColor()
        searchController.searchBar.tintColor = UIColor.redColor()
        // Place the search bar view to the tableview headerview.
        tblSearchResults.tableHeaderView = searchController.searchBar
    }

    func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
        shouldShowSearchResults = true
        tblSearchResults.reloadData()
    }


    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
        shouldShowSearchResults = false
        tblSearchResults.reloadData()
    }

    func searchBarSearchButtonClicked(searchBar: UISearchBar) {
        if !shouldShowSearchResults {
            shouldShowSearchResults = true
            tblSearchResults.reloadData()
        }

        searchController.searchBar.resignFirstResponder()
    }

    func updateSearchResultsForSearchController(searchController: UISearchController) {
        let searchString = searchController.searchBar.text

        // Filter the data array and get only those countries that match the search text.
        filteredArray = itemNameArray.filter({ (item_name) -> Bool in
            let countryText: NSString = item_name

            return (countryText.rangeOfString(searchString!, options: NSStringCompareOptions.CaseInsensitiveSearch).location) != NSNotFound
        })

        // Reload the tableview.
        tblSearchResults.reloadData()
    }

}

最佳答案

尝试删除下面的行

  tableView.deselectRowAtIndexPath(indexPath, animated: true)

来自您的didSelectRowAtIndexPath

关于ios - prepareForSegue 将从数组中提取的错误(延迟)字符串发送到第二个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39524235/

相关文章:

ios - 修改 Collection View 单元格内容的图层属性

swift2 - Swift 2.0、Xcode 7 问题

ios - 在 iPhone 上使用 CADisplayLink 在 OpenGL ES View 中进行恒速旋转

iphone - 将Integer设置为除零以外的任何值时为EXC_BAD_ACCESS

ios - Pushkit VoiP 推送通知唤醒应用执行时间

ios - Alamofire .responseJSON 不适用于 JSON 响应

swift - 不一致的 SceneKit 帧率

多任务处理中的 iOS 9 应用程序提交问题需要所有方向

ios - 如何在 Objective c 中推送到 View Controller 时显示 ActivityIndi​​cator

ios - 如何在 Realm 迁移中执行查询?