1

I'm starter in swift. I create tableview and get data from jsonFile to show text and picture. Then I want to add searchBar on tableview but have problem.

import UIKit

    class EpisodesTableViewController: UITableViewController
    {
        var episodes = [Episode]()

        var names = [Episode]()

        let searchController = UISearchController(searchResultsController: nil)
        var filteredNames = [Episode]()

        func filterContentForSearchText(searchText: String) {
            filteredNames = self.names.filter { name in
                return name.title!.lowercaseString.containsString(searchText.lowercaseString)
            }
            tableView.reloadData()
        }





        override func viewDidLoad()
        {
            super.viewDidLoad()


            searchController.searchResultsUpdater = self
            searchController.dimsBackgroundDuringPresentation = false
            definesPresentationContext = true
            tableView.tableHeaderView = searchController.searchBar

            tableView.setContentOffset(CGPoint(x: 0, y: searchController.searchBar.frame.size.height), animated: false)

            tableView.estimatedRowHeight = tableView.rowHeight
            tableView.rowHeight = UITableViewAutomaticDimension
            tableView.separatorStyle = .None

            self.episodes = Episode.downloadAllEpisodes()
            self.tableView.reloadData()
        }

        override func preferredStatusBarStyle() -> UIStatusBarStyle {
            return .LightContent
        }

        // MARK: - Table view data source

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

        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
        {
            if searchController.active && searchController.searchBar.text != ""{
                return filteredNames.count
            }else{
                return names.count
            }
            return episodes.count
        }

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

            let cell = tableView.dequeueReusableCellWithIdentifier("Episode Cell", forIndexPath: indexPath) as! EpisodeTableViewCell
            let episode = self.episodes[indexPath.row]

            let data: Episode

            if searchController.active && searchController.searchBar.text != "" {
                data = filteredNames[indexPath.row]
            }
            else {
                data = names[indexPath.row]
            }

            let titleName = data.title!

            cell.episode = episode
            cell.textLabel?.text = titleName

            return cell
        }

        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
            if segue.identifier == "SendData"{
                if let detailPage = segue.destinationViewController as? detailEpisodeViewController {
                    if let indexpath = tableView.indexPathForSelectedRow {
                        let episode = episodes[indexpath.row]
                        detailPage.episode = episode
                    }
                }
        }
    }

}


    extension EpisodesTableViewController: UISearchResultsUpdating {

        func updateSearchResultsForSearchController(searchController: UISearchController) {
            filterContentForSearchText(searchController.searchBar.text!)
        }
    }

this my code.

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
        {
            if searchController.active && searchController.searchBar.text != ""{
                return filteredNames.count
            }else{
                return names.count
            }
            return episodes.count
        }

When I return filteredNames and names interface just show seachbar. If I return filtered names and episodes show error index out of range. I don't know How to fix that.

1
  • 1
    return exists the method, you can't have consecutive return statements like this. The moment one of them are hit, the value is returned and the methods is exited. You can return tuples, structs, objects, or collections as a way of returning multiple pieces of data, but in this case, you must return a single Int to satisfy the method signature. Commented Aug 18, 2016 at 15:57

1 Answer 1

1

If you want to return two values just return a touple like so:

return (DataType, DataType)

so this could be

func returnTouple() -> (String, AnyObject) {
    return ("Hello World", 1)
}

then you would access it like so:

let (myString, myObject) = returnTouple()

and myString == "Hello World"

You could also access both throught .0 and .1 like returnTouple().0 == "Hello World"

Next,

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if searchController.active && searchController.searchBar.text != "" {
        return filteredNames.count
    } else {
        return names.count
    }
    return episodes.count
}

This function shouldn't work. You have an if {} else {} with a return statement in both sections. Unless you said if {} else if {} this makes the thrid return statement impossible to hit so it shouldn't be there.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.