2017-04-25 99 views
0

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

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

+1

那么你的问题是什么? – Sweeper

+0

如何用SesrchController实现UISearchBar –

回答