搜索两个字符串集合 Swift
Posted
技术标签:
【中文标题】搜索两个字符串集合 Swift【英文标题】:Search two string collections Swift 【发布时间】:2017-04-25 15:44:29 【问题描述】:我有一个使用 https://newsapi.org 的新闻应用程序,它使用大约 60 个来源,因此目录很长。因为我的swift编程不太好,所以我做了一个很难搜索的解决方案,我在互联网上找不到任何解决方案。但是我需要实现一个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,它看起来像这样:
我会对每一个提示感到非常高兴,我已经为这个问题困扰了好几个星期。提前致谢,M.Rest
【问题讨论】:
那么你的问题是什么? 如何用 SesrchController 实现 UISearchBar 【参考方案1】:您只需要正确实现numberOfRowsInSection
,以便在使用时在搜索栏中键入文本并根据搜索文本过滤数组。
以下示例可能会有所帮助。如果您需要任何帮助,请告诉我。
http://www.appcoda.com/how-to-add-search-bar-uitableview/
【讨论】:
以上是关于搜索两个字符串集合 Swift的主要内容,如果未能解决你的问题,请参考以下文章