如何在swift中返回多个值

时间:2021-07-23 02:01:37

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.

我很快就开始了。我创建tableview并从jsonFile获取数据以显示文本和图片。然后我想在tableview上添加searchBar但有问题。

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.

当我返回filteredNames和名称界面时,只显示seachbar。如果我返回过滤后的名称和剧集,则显示错误索引超出范围。我不知道如何解决这个问题。

1 个解决方案

#1


1  

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

如果你想返回两个值,只需返回如下:

return (DataType, DataType)

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"

和myString ==“Hello World”

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

您还可以访问.0和.1之类的函数,如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.

此功能不起作用。在两个部分中都有一个if {} else {}和return语句。除非你说{} else if {}这使得thrid return语句无法命中,所以它不应该存在。

#1


1  

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

如果你想返回两个值,只需返回如下:

return (DataType, DataType)

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"

和myString ==“Hello World”

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

您还可以访问.0和.1之类的函数,如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.

此功能不起作用。在两个部分中都有一个if {} else {}和return语句。除非你说{} else if {}这使得thrid return语句无法命中,所以它不应该存在。