티스토리 뷰
출처 : http://qiita.com/fumiyasac@github/items/02a7b962e9a2013c56a0
Swift 2.2를 사용해서 샘플 iOS어플을 만들어 봅시다.
출처소스를 2.2에 맞게 변경하였습니다.
작성한 어플은 RSS리더입니다.
RSS리더는 TableView를 사용하고 데이터 통신도 있습니다.
이번에는 심플한 조선일보를 표시하고 내용은 Safari를 띄워서 표시하는 것으로 합니다.

우선은 Xcode 7.3을 실행하고 Single View Application을 선택합니다.
언어는 물론 Swift입니다.
Main.storyboard의 View위에 TableView를 배치합니다.
TableView를 우클릭해서 dataSource와 delegate를 ViewController에 연결합니다.
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, NSXMLParserDelegate
이번에는 NSXMLParser(XML파서 표준라이브러리)를 사용합니다.
let feedUrl : NSURL = NSURL(string: "http://www.chosun.com/site/data/rss/rss.xml")!
RSS URL을 지정합니다.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let parser : NSXMLParser = NSXMLParser(contentsOfURL: feedUrl)!
parser.delegate = self;
parser.parse()
}
NSXMLParserDelegate프로토콜을 상속하고있기 때문에, NSXMLParser오브젝트를 작성해서 파싱처리를 자기자신에게 delegate합니다.
var items : [Item] = [Item]()
class Item {
var title : String!
var url : String!
}
RSS의 item을 Item클래스에 포함하고 배열로 저장합니다.
Code
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, NSXMLParserDelegate {
let feedUrl : NSURL = NSURL(string: "http://www.chosun.com/site/data/rss/rss.xml")!
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
@IBOutlet weak var tableView: UITableView!
var items : [Item] = [Item]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let parser : NSXMLParser = NSXMLParser(contentsOfURL: feedUrl)!
parser.delegate = self;
parser.parse()
}
override func viewDidLayoutSubviews() {
tableView.frame = CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-20)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
let item = items[indexPath.row]
cell.textLabel?.text = item.title
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let item = items[indexPath.row]
UIApplication.sharedApplication().openURL(NSURL(string: item.url)!)
}
var currentElementName : String!
let itemElementName = "item"
let titleElementName = "title"
let linkElementName = "link"
func parserDidStartDocument(parser: NSXMLParser)
{
}
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String])
{
currentElementName = nil
if elementName == itemElementName {
items.append(Item())
} else {
currentElementName = elementName
}
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?)
{
currentElementName = nil;
}
func parser(parser: NSXMLParser, foundCharacters string: String)
{
if currentElementName != nil {
if items.count > 0 {
let lastItem = items[items.count-1]
if currentElementName == titleElementName {
let tmpString = lastItem.title
lastItem.title = (tmpString != nil) ? tmpString! + string : string
} else if currentElementName == linkElementName {
lastItem.url = string
}
}
}
}
func parserDidEndDocument(parser: NSXMLParser)
{
tableView.reloadData()
}
class Item {
var title : String!
var url : String!
}
}
'Mobile > iOS' 카테고리의 다른 글
| [iOS] 필수로 사용하는 아이콘 사이즈. (0) | 2016.04.18 |
|---|---|
| [예제]Captcha Generator for iOS (0) | 2016.04.08 |
| iOS9의 화면사이즈 (0) | 2016.04.06 |
| Swift2.0이상에서 XML를 UITableView에 리스트를 표시하는 샘플 추가사항 (0) | 2016.04.06 |
| [MAC]Page Down Key는 어디에? (0) | 2016.04.04 |
- Total
- Today
- Yesterday
- mysql
- iPhone
- 성경통독
- 통독
- genesis
- 아이폰
- flutter
- youtube
- 반응형웹
- 성경책
- IOS
- AWS
- 말씀
- 성경
- bible
- SWIFT
- 창세기
- 성경읽기
- 안드로이드
- VR
- 유튜브
- MariaDB
- react
- 1일1독
- 오디오북
- 성경듣기
- 아이폰개발
- 플러터
- Android
- ChatGPT
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
| 29 | 30 | 31 |