gpt4 book ai didi

ios - Swift 搜索两个字符串集合

转载 作者:行者123 更新时间:2023-11-30 12:30:31 25 4
gpt4 key购买 nike

我有一个新闻应用程序,它使用 https://newsapi.org ,它使用大约 60 个源,因此目录相当长。因为我不太擅长快速编程,所以我制定了一个很难搜索的解决方案,并且在互联网上找不到任何解决方案。但我需要实现一个 UISearchBar,否则它不是很用户友好。这是我的两个字符串集合,源名称和 URL。

var sourceURL = [
"abc-news-au",
"ars-technica",
"associated-press",
"bbc-news",
"bbc-sport",
"bild",
"bloomberg",
"business-insider",
"business-insider-uk",
"buzzfeed",
"cnbc",
"cnn",
"daily-mail",
"engadget",
"entertainment-weekly",
"espn",
"espn-cric-info",
"financial-times",
"focus",
"football-italia",
"fortune",
"four-four-two",
"fox-sports",
"google-news",
"gruender-szene",
"hacker-news",
"ign",
"independent",
"mashable",
"metro",
"mirror",
"mtv-news",
"mtv-news-uk",
"national-geographic",
"new-scientist",
"newsweek",
"new-york-magazine",
"nfl-news",
"polygon",
"recode",
"reddit-r-all",
"reuters",
"sky-news",
"sky-sports-news",
"spiegel-online",
"t3n",
"talksport",
"techcrunch",
"techradar",
"the-economist",
"the-guardian-au",
"the-guardian-uk",
"the-hindu",
"the-huffington-post",
"the-lad-bible",
"the-new-york-times",
"the-next-web",
"the-sport-bible",
"the-telegraph",
"the-times-of-india",
"the-verge",
"the-wall-street-journal",
"the-washington-post",
"timet",
"usa-today",
"wired-de",
""


]

var sourceName = [

"AustralianBroadcastCorporation",
"ArsTechnica",
"AP",
"BBC",
"BBCSport",
"Bild",
"Bloomberg",
"BusinessInsider",
"BusinessInsiderUK",
"BuzzFeed",
"CNBC",
"CNN",
"Daily Mail",
"Engadget",
"EntertainmentWeekly",
"ESPN",
"ESPNCricInfo",
"FinancialTimes",
"Focus",
"FootballItalia",
"Fortune",
"FourFourTwo",
"FoxSports",
"Google",
"GründerSzene",
"HackerNews",
"IGN",
"Independent",
"Mashable",
"Metro",
"Mirror",
"MTVNews",
"MTVNewsUK",
"NatGeo",
"NewScientist",
"NewsWeek",
"NewYorkMagazine",
"NFLNews",
"Polygon",
"Recode",
"Reddit",
"Reuters",
"SkyNews",
"SkySportsNews",
"SpiegelOnline",
"t3n",
"TalkSport",
"TechCrunch",
"TechRadar",
"TheEconomist",
"TheGuardianAU",
"TheGuardianUK",
"TheHindu",
"HuffPo",
"TheLadBible",
"The New York Times",
"TNW",
"The SportBible",
"The Telegraph",
"The Times Of India",
"The Verge",
"WSJ",
"The Watshington Post",
"Time",
"USA",
"Wired",
"Powered by NewsAPI.org"


]

名称和 URL 位于同一 IndexPath 上,因此当您单击名称时,它会加载 URL。代码如下所示:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.sourceName.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = UITableViewCell()

cell.textLabel?.text = sourceName[indexPath.row]

cell.textLabel?.font = UIFont.systemFont(ofSize: 20, weight: UIFontWeightHeavy)

cell.textLabel?.textAlignment = NSTextAlignment.center

cell.backgroundColor = UIColor.clear

return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

if sourceURL[indexPath.row] != "" {

sourceStruct.source = sourceURL[indexPath.row]

sourceStruct.sourceTitle = sourceName[indexPath.row]

let loginPageView = (self.storyboard?.instantiateViewController(withIdentifier: "NewsHome"))! as UIViewController
self.present(loginPageView, animated: true, completion: nil)

} else {

let vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ArticleWV") as! ArticleWV

vc.url = "https://newsapi.org"

self.present(vc, animated: true, completion: nil)

}
}

使用 UI,它看起来像这样:

Screenshot of the UI

我会对每一个提示感到非常高兴,我已经在这个问题上坚持了好几个星期了。预先感谢,M.Rest

最佳答案

您只需要正确实现numberOfRowsInSection,以便在使用时在搜索栏中键入文本并根据搜索文本过滤数组。

下面的示例可能会有所帮助。如果您需要任何帮助,请告诉我。

http://www.appcoda.com/how-to-add-search-bar-uitableview/

关于ios - Swift 搜索两个字符串集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43615317/

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