2017-04-25 172 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 –

回答