首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >搜索两个字符串集合Swift

搜索两个字符串集合Swift
EN

Stack Overflow用户
提问于 2017-04-25 23:44:30
回答 1查看 354关注 0票数 0

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

代码语言:javascript
复制
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"


]

名字和网址在同一个IndexPath上,所以当你点击一个名字时,它就会加载网址。代码如下所示:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

发布于 2017-04-25 23:51:11

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

下面的例子可能会很有帮助。如果你需要任何帮助,请告诉我。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43615317

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档