Last active
November 26, 2015 11:46
-
-
Save fumiyasac/44d4c1dab760b91b052a to your computer and use it in GitHub Desktop.
Swift2.0でレスポンスのXMLをUITableViewに一覧表示をするサンプル ref: http://qiita.com/fumiyasac@github/items/02a7b962e9a2013c56a0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//NSXMLParserDelegateを記載 | |
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, NSXMLParserDelegate | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) { | |
if (elementName as NSString).isEqualToString(self.itemElementName) { | |
//各メンバ変数がnilでなければメンバ変数elementsへ「キー」と「値」のペアを格納 | |
if !self.name.isEqual(nil) { | |
self.elements.setObject(self.name, forKey: self.nameElementName) | |
} | |
if !self.maker.isEqual(nil) { | |
self.elements.setObject(self.maker, forKey: self.makerElementName) | |
} | |
if !self.price.isEqual(nil) { | |
self.elements.setObject(self.price, forKey: self.priceElementName) | |
} | |
if !self.type.isEqual(nil) { | |
self.elements.setObject(self.type, forKey: self.typeElementName) | |
} | |
if !self.url.isEqual(nil) { | |
self.elements.setObject(self.url, forKey: self.urlElementName) | |
} | |
if !self.image.isEqual(nil) { | |
self.elements.setObject(self.image, forKey: self.imageElementName) | |
} | |
self.posts.addObject(self.elements) | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//画像 ※image要素のデータを取得した後にnilの可能性をチェック | |
let q_global: dispatch_queue_t = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); | |
let q_main: dispatch_queue_t = dispatch_get_main_queue(); | |
let imageParameter: String! = (posts.objectAtIndex(indexPath.row).valueForKey("image")!.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)) | |
//URLがあれば画像取得処理を実行 | |
if imageParameter != "" { | |
//サムネイルのURLをもとに画像データ(NSData型)を作成 | |
let imageURL = NSURL(string: imageParameter) | |
//非同期でURLデータを取得 | |
dispatch_async(q_global,{ | |
//サムネイルのURLをもとに画像データ(NSData型)を作成 | |
var error: NSError? | |
var imageData: NSData? | |
//データが取得できれば正常処理 | |
do { | |
imageData = try NSData(contentsOfURL: imageURL!, options: []) | |
//Errorが返された場合はimageDataにnilを入れる | |
} catch let error1 as NSError { | |
error = error1 | |
imageData = nil | |
//それ以外の例外 | |
} catch { | |
fatalError() | |
} | |
if error != nil { | |
//nilの時はデフォルトイメージを表示してあげる | |
let image: UIImage = UIImage(named: "no_image.gif")! | |
cell!.okashiImage?.image = image | |
} | |
//更新はメインスレッドで行う | |
dispatch_async(q_main,{ | |
//イメージデータがnilでなければサムネイル画像を表示 | |
if((imageData) != nil){ | |
//xibのサムネイルエリアに表示する | |
let image: UIImage = UIImage(data: imageData!)! | |
cell!.okashiImage?.image = image | |
cell!.layoutSubviews() | |
} | |
}) | |
}) | |
} else { | |
//nilの時はデフォルトイメージを表示してあげる | |
let image: UIImage = UIImage(named: "no_image.gif")! | |
cell!.okashiImage?.image = image | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//XMLのフィード取得用URL | |
let feedUrl : NSURL = NSURL(string:"http://www.sysbird.jp/webapi/?apikey=guest&max=30&order=r")! | |
//XMLの現在要素名を入れる変数 | |
var currentElementName : String! | |
//取得する要素名(とりはじめの要素) | |
let itemElementName : String = "item" | |
//取得する要素名の決定(item要素の下にあるもの) | |
let nameElementName : String = "name" | |
let makerElementName : String = "maker" | |
let priceElementName : String = "price" | |
let typeElementName : String = "type" | |
let urlElementName : String = "url" | |
let imageElementName : String = "image" | |
//各エレメント用の変数 | |
var posts = NSMutableArray() | |
var elements = NSMutableDictionary() | |
var element = NSString() | |
var name = NSMutableString() | |
var maker = NSMutableString() | |
var price = NSMutableString() | |
var type = NSMutableString() | |
var url = NSMutableString() | |
var image = NSMutableString() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: [String : String]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment