gpt4 book ai didi

ios - 如何快速返回多个值

转载 作者:行者123 更新时间:2023-11-28 06:36:19 24 4
gpt4 key购买 nike

我是 swift 的初学者。我创建 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!)
}
}

这是我的代码。

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
}

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

最佳答案

如果你想返回两个值,只需返回一个元组,如下所示:

返回(数据类型,数据类型)

这可能是

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

然后你会像这样访问它:

let (myString, myObject) = returnTouple()

myString == "Hello World"

您还可以像 returnTouple().0 == "Hello World" 一样访问 .0 和 .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
}

这个函数不应该起作用。您在 if {} else {} 的两个部分中都有一个 return 语句。除非你说 if {} else if {} 这使得第三个 return 语句不可能命中,所以它不应该在那里。

关于ios - 如何快速返回多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39022479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com