1

現在Swift 3でRSSリーダーを開発をしているのですが、一つのURLからの表示ができたのですが複数のURLで表示させようとするとその配列の中の一つしか表示されません。
JSON変換はGoogleのAPIを使っています。
コードはこちらです。

class MatomeVieController: UIViewController , UITableViewDelegate, UITableViewDataSource {
    var NewDataArray = NSArray()

    var myTableView : UITableView!

    var matomeURL = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=表示させたいURL&num=100"

    var URLstring : [String] = [
        "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=表示させたいUR&num=100",
        "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=表示させたいUR&num=100",
        "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=表示させたいUR&num=100"
    ]
    var isLoading : Bool = false

    override func viewDidLoad() {
        super.viewDidLoad()

        ...
        GetURLSRSS()
        ...
    }

    func GetURLSRSS() {
        for URLstrings in URLstring {
            if let requestURL2 = Foundation.URL(string: URLstrings) {
                Alamofire.request(requestURL2, method: .get ,parameters: nil).responseJSON {
                    response in
                    if let json = response.result.value {
                        let jsonDic = json as! NSDictionary
                        let responseData = jsonDic["responseData"] as! NSDictionary
                        let feed = responseData["feed"] as! NSDictionary
                        self.NewDataArray = feed["entries"] as! NSArray
                    }
                    self.myTableView.reloadData()
                }
            }
        }
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }

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

        let NewDic = NewDataArray[indexPath.row] as! NSDictionary
        let NewURL = NewDic["link"] as! String
        url = Foundation.URL(string: NewURL)
        let Deteilwebview = UINavigationController(rootViewController: WebViewController())
    }

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

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

        // 再利用するCellを取得する.
        let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath) as! ItemCell

        let Newdic = NewDataArray[indexPath.row] as! NSDictionary
        let title = Newdic["title"] as? String
        let publishedDate = Newdic["publishedDate"] as? String
        let links = Newdic["link"] as! String
        let timedate = self.convertDateFormat(publishedDate!)


        cell.title.text = title
        cell.author.text = "サイト名"
        cell.time.text = timedate
        cell.link = links

        return cell
    }
}

複数のサイトを一つのViewで表示するにはどのように表現したら良いでしょうか?

1 件の回答 1

1

下記のところで、NewDataArrayに代入してしまっているので、後から受信したデータで先に受信したデータが上書きされてしまっています。

self.NewDataArray = feed["entries"] as! NSArray

受信したデータをそれぞれ1つにまとめて表示するには、上書きするのではなくて、それぞれを1つにまとめる必要があります。

let entries = feed["entries"] as! NSArray
self.NewDataArray.addObjects(from: entries)

たとえば上記のようにします。
このとき、NewDataArrayvar NewDataArray = NSArray()のようにNSArrayのインスタンスなので、そのままではデータを追加することができません。上記のようにするにはvar NewDataArray = NSMutableArray()のように変更可能なNSMutableArrayを使う必要があります。

余談ですが、もともとNSArrayを使われていたのとfeed["entries"]の型が不明なので、そのままNSMutableArrayとだけ変更しましたが、SwiftではNSArrayNSMutableArrayを使用するメリットはないので、Swift標準ライブラリに用意されているArrayを使う方が(型安全だったりするので)良いです。NSDictionaryも同様でDictionaryを使いましょう。

2
  • 編集や改善点をご教授いただきありがとうございます。無事に表示することができました!そこでなのですが、表示したCellを日付順に並び替えたいのですがその場合はどのようにしたら良いのでしょうか? 2016年11月15日 9:24
  • 1
    @ぽんこつエンジニア 面倒でも異なるトピックの場合は新しい質問として投稿してください。サイトの方針でもあり、他の人のためでもあります😉。それと、この質問は解決したなら回答を承‌​認してください。そうしないと、いつまでも‌​未解決の質問で定期的に上がってきてしまい‌​ます。 2016年11月15日 10:18

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。