###前提・実現したいこと
現在、独学でswiftを学習しており、RSSを使った2chまとめリーダのようなものを作っています。
TableViewの各セルに記事のタイトルを表示し、それをタップするとWebViewを使って記事を表示するところまで実装できています。
現在、1つのサイトのRSSしか使っていないのですが、実際の2chまとめリーダのように複数のサイトのRSSデータを取得し、それをランダムに表示したいと考えています。
ネットで調べても実装方法が出てこず、文系の学生のため周りに聞く人がおらず困っています。
お力添え、よろしくお願いします。
###該当のソースコード
学習を初めてまだ1ヶ月のため、稚拙なコードで申し訳ございません。
TableViewController
import UIKit
class TableViewController: UITableViewController,XMLParserDelegate {
var parser:XMLParser! var items = [Item]() var item:Item? var currentString = "" class Item{ var title = "" var link = "" } override func viewDidLoad() { super.viewDidLoad() // Uncomment the following line to preserve selection between presentations // self.clearsSelectionOnViewWillAppear = false // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func reloadButton(_ sender: Any) { self.startdownload() self.tableView.reloadData() } override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 100 } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { //#warning Incomplete implementation, return the number of rows return items.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) cell.textLabel?.text = items[indexPath.row].title // Configure the cell... return cell } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) startdownload() } func startdownload(){ self.items = let url_text = "http://alfalfalfa.com/index.rdf" guard let url = NSURL(string: url_text) else{ return } // インターネット上のXMLを取得し、NSXMLParserに読み込む guard let parser = XMLParser(contentsOf: url as URL) else{ return } parser.delegate = self; parser.parse() } func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) { self.currentString = ""//一旦空に if elementName == "item"{ self.item = Item() } } func parser(_ parser: XMLParser, foundCharacters string: String) { self.currentString += string } func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) { switch elementName{ case "title":self.item?.title = currentString case "link":self.item?.link = currentString case "item":self.items.append(self.item!) default:break } } func parserDidEndDocument(_ parser: XMLParser) { self.tableView.reloadData() }
###試したこと
インターネットで調べた
###補足情報(言語/FW/ツール等のバージョンなど)
Xcode 8.3.3
あなたの回答
tips
プレビュー