Created
April 7, 2017 09:01
-
-
Save harryhan24/491c94749c8ac3a3a53b311e75c5dd2a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| ```swift | |
| import UIKit | |
| class MainVC: UIViewController, UITableViewDelegate, UITableViewDataSource { | |
| @IBOutlet weak var tableView: UITableView! | |
| var partyRocks = [PartyRock]() | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| let p1 = PartyRock(imageURL: "http://www.wavefm.com.au/images/stories/2015/04/redfoo.jpg", videoURL: "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/ev4bY1SkZLg\" frameborder=\"0\" allowfullscreen></iframe>", videoTitle: "Lights Out") | |
| let p2 = PartyRock(imageURL: "http://www.croshalgroup.com/wp-content/uploads/2015/05/Redfoo-Website.jpg", videoURL: "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/1w9DiGlZksU\" frameborder=\"0\" allowfullscreen></iframe>", videoTitle: "Let's Get Ridiculous") | |
| let p3 = PartyRock(imageURL: "https://i.ytimg.com/vi/2wUxw6GH3IM/hqdefault.jpg", videoURL: "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/vg_nvEGryA4\" frameborder=\"0\" allowfullscreen></iframe>", videoTitle: "Juicy Wiggle Lesson") | |
| let p4 = PartyRock(imageURL: "http://www.billboard.com/files/styles/article_main_image/public/media/lmfao-party-rock-anthem-2011-billboard-650.jpg", videoURL: "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/gZIqW92GaTQ\" frameborder=\"0\" allowfullscreen></iframe>", videoTitle: "Party Rock Commercial") | |
| let p5 = PartyRock(imageURL: "http://direct-ns.rhap.com/imageserver/v2/albums/Alb.219913217/images/600x600.jpg", videoURL: "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/tWyuglGCKgE\" frameborder=\"0\" allowfullscreen></iframe>", videoTitle: "Juicy Wiggle") | |
| //함수를 넣어주자 | |
| partyRocks.append(p1) | |
| partyRocks.append(p2) | |
| partyRocks.append(p3) | |
| partyRocks.append(p4) | |
| partyRocks.append(p5) | |
| //델리게이션을 설정해주자 | |
| tableView.delegate = self | |
| tableView.dataSource = self | |
| } | |
| //cellForRowAt의 경우엔 각 셀들에 대해서 값을 어떻게 처리해줄지에 대한 내용을쓴다. | |
| //본문에서는 | |
| func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
| if let cell = tableView.dequeueReusableCell(withIdentifier: "PartyCell", for: indexPath) as? PartyCell { | |
| let partyRock = partyRocks[indexPath.row] | |
| cell.updateUI(partyRock: partyRock) | |
| return cell | |
| } else { | |
| return UITableViewCell() | |
| } | |
| } | |
| func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
| return partyRocks.count | |
| } | |
| func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
| let partyRock = partyRocks[indexPath.row] | |
| performSegue(withIdentifier: "VideoVC", sender: partyRock) | |
| } | |
| //세그가 실행되기 직전에 실행된다. | |
| override func prepare(for segue: UIStoryboardSegue, sender: Any?) { | |
| //if let을 쓰는 이유는 safe하게 unwrapping 하기 위함 | |
| if let destination = segue.destination as? VideoVC { | |
| //sender는 caller 즉 부른곳이다. | |
| if let party = sender as? PartyRock { | |
| destination.partyRock = party | |
| } | |
| } | |
| } | |
| ``` | |
| ```swift | |
| performSegue(withIdentifier: "VideoVC", sender: partyRock) | |
| //인터페이스 빌드에서 컨트롤+드래그로 세그를 만들어 준 후 해당 세그에 값을 부여해준 후 이름값을 출력하여준다. | |
| ``` | |
|  | |
|  | |
| 돌아갈땐 dimiss | |
| ## 데이터전달 | |
| ```swift | |
| override func prepare(for segue: UIStoryboardSegue, sender: Any?) { | |
| if let destination = segue.destination as? VideoVC { | |
| if let party = sender as? PartyRock { | |
| destination.partyRock = party | |
| } | |
| } | |
| } | |
| ``` | |
| prepare 함수의 경우 segue 이동이 시작되기 직전 실행되며 세그에 대 한 정보는 for segue: UIStoryboardSegue를 통해 | |
| 스토리보드에서 가져온다. segue 목표가 VideoVC이고 sender가 partyRock이면 목표의 partRock에 part 값을 넣어준다? |
This file contains hidden or 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
| // | |
| // VideoVC.swift | |
| // PartyRock | |
| // | |
| // Created by harry on 2017. 4. 4.. | |
| // Copyright © 2017년 harry. All rights reserved. | |
| // | |
| import UIKit | |
| class VideoVC: UIViewController { | |
| @IBOutlet weak var webView: UIWebView! | |
| @IBOutlet weak var titleLbl: UILabel! | |
| //partyRock 객체로 초기화 | |
| private var _partyRock: PartyRock! | |
| var partyRock: PartyRock{ | |
| get{ | |
| return _partyRock | |
| }set{ | |
| _partyRock = newValue | |
| } | |
| } | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| titleLbl.text = partyRock.videoTitle | |
| webView.loadHTMLString(partyRock.videoURL, baseURL: nil) | |
| // Do any additional setup after loading the view. | |
| } | |
| override func didReceiveMemoryWarning() { | |
| super.didReceiveMemoryWarning() | |
| // Dispose of any resources that can be recreated. | |
| } | |
| } |
This file contains hidden or 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
| ```siwft | |
| // | |
| // PartyCell.swift | |
| // PartyRock | |
| // | |
| // Created by harry on 2017. 4. 4.. | |
| // Copyright © 2017년 harry. All rights reserved. | |
| // | |
| import UIKit | |
| class PartyCell: UITableViewCell { | |
| @IBOutlet weak var videoPreviewImage: UIImageView! | |
| @IBOutlet weak var videoTitle: UILabel! | |
| override func awakeFromNib() { | |
| super.awakeFromNib() | |
| // Initialization code | |
| } | |
| //PartyRock 형식의 partyrock을 import 하여 준다. | |
| func updateUI(partyRock: PartyRock){ | |
| videoTitle.text = partyRock.videoTitle | |
| let url = URL(string: partyRock.imageURL)! | |
| DispatchQueue.global().async { | |
| //데이터를 보여주기 위해선 해당 이미지를 로컬에 다운받아주어야 한다. | |
| do{ | |
| let data = try Data(contentsOf: url) | |
| DispatchQueue.global().sync { | |
| self.videoPreviewImage.image = UIImage(data: data) | |
| } | |
| }catch{ | |
| //handle the error | |
| } | |
| } | |
| } | |
| } | |
| ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment