S
S
Stanislav Korolevskiy2016-05-25 22:26:23
Apple Xcode
Stanislav Korolevskiy, 2016-05-25 22:26:23

Why doesn't the transition work after clicking on a cell as a result of a table search?

Set up search in tableView. everything works, the table is sorted by the result of the input, but when you click on the cell, the application crashes (fatal error: unexpectedly found nil while unwrapping an Optional value). Without the searchBar, everything clicks, everything works. How to be? There is an assumption that imageView is guilty, because. I sort the table only by String array of names.

func updateSearchResultsForSearchController(searchController: UISearchController) {
        self.filteredRal = self.ralNames.filter { (ral: String) -> Bool in
            if ral.lowercaseString.containsString(self.searchController.searchBar.text!.lowercaseString) {
                return true
            } else {
                return false
            }
        }
        
        self.resultsController.tableView.reloadData()
    }

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == self.tableView {
            return self.ralNames.count
        } else {
            return self.filteredRal.count
        }
 
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cellIdentifier = "Cell"
        
        let cell = self.tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MainTableViewCell

        // Configure the cell...
        
        if tableView == self.tableView {
            cell.titleLabel?.text = self.ralNames[indexPath.row]
        } else {
            cell.titleLabel?.text = self.filteredRal[indexPath.row]
        }
        
        cell.cellImage?.image = UIImage(named: ralImages[indexPath.row])

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

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showDetailsSegue" {
            if let indexPath = self.tableView.indexPathForSelectedRow {
                let destinationVC = segue.destinationViewController as! DetailsViewController
                destinationVC.ralImage = self.ralImages[indexPath.row]
                destinationVC.ralName = self.ralNames[indexPath.row]
            }
        }

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Maxim Globak, 2016-05-26
@maximglobak

Describe how the didSelectRowAtIndexPath method works for you. Most likely, when you click on a cell, you take data from the ralNames array, but you need it from filteredRal.

G
GoGetPrice, 2016-05-26
@GoGetPrice

You send the data of the wrong array in this Segue, you need to send the data of the search array there.
Do a check for search activity, if it is active, then transfer data from one array, if not, then another.
And it's better to do it through didSelectRowAtIndexPath.

S
Stanislav Korolevskiy, 2016-05-26
@korolevsky_s

Now entered. My search result is displayed in the resultController, so I saved this dregs...))) Yes, you need to finish it, but at least everything works. ) Almost everything... when searching, the order of the pictures in the table gets lost...

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showDetailsSegue" && searchController.active {
            if let indexPath = self.resultsController.tableView.indexPathForSelectedRow {
                let destinationVC = segue.destinationViewController as! DetailsViewController
                destinationVC.ralImage = self.ralImages[indexPath.row]
                destinationVC.ralName = self.filteredRal[indexPath.row]
            } else if let indexPath = self.tableView.indexPathForSelectedRow {
                let destinationVC = segue.destinationViewController as! DetailsViewController
                destinationVC.ralImage = self.ralImages[indexPath.row]
                destinationVC.ralName = self.ralNames[indexPath.row]
            }
        }
        
        if segue.identifier == "showDetailsSegue" {
            if let indexPath = self.tableView.indexPathForSelectedRow {
                let destinationVC = segue.destinationViewController as! DetailsViewController
                destinationVC.ralImage = self.ralImages[indexPath.row]
                destinationVC.ralName = self.ralNames[indexPath.row]
            }
        
        }
    }

F
FlooDwm, 2016-10-17
@FlooDwm

Found a solution!

let cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question