Swift의 TableView 화면 변환 편

13622 단어 Swift
이것은 이전 글의 Swift에서 TableView 데이터 표시 편의 후속 내용이다.
http://qiita.com/senseiswift/items/9b5476531a843b0e314a

Swift의 간단한 표 보기 화면 변환 편


할 일

  • 새 파일 SecondViewController
  • 추가
  • StoryBoard에 새 ViewController
  • 구성
  • StoryBoard의 새 ViewController의 Custom Class에서 "SecondViewController"
  • 를 지정합니다.
  • StoryBoard의 SecondViewController에 TableView
  • 구성
  • StoryBoard의 TableView를 파일에 연결하는 SecondViewController
  • StoryBoard의 ViewController와 SecondViewController를 연결하고 Identifer를'ShowSecondView'
  • 로 설정합니다.
  • ViewController에 전달할 문자열을 SecondView에 추가
  • 단원의 헤더를 뽑을 때 전달되는 문자열을 설정하고 Segue
  • 를 호출합니다
  • SecondViewController에서 받은 문자열 변수 추가
  • 칸에 받은 문자열 보이기
  • ViewController의preparefor Segue에서 문자열을SecondViewController
  • 에 전달

    3. 새 파일 SecondViewController 추가



    6. StoryBoard의 ViewController와 SecondeviewController를 연결하고 Identifer를'Show SecondView', Segue를'Show'로 설정



    ViewController
    import UIKit
    
    class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
        @IBOutlet weak var tableView: UITableView!
    
        // 7. SecondViewに渡す文字列
        var selectedText: String?
    
        // テーブルに表示するテキスト
        let texts = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            tableView.delegate = self
            tableView.dataSource = self
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        // セルの行数
        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return texts.count
        }
    
        // セルのテキストを追加
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
    
            cell.textLabel?.text = texts[indexPath.row]
            return cell
        }
    
        func tableView(table: UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath) {
            println(texts[indexPath.row])
    
            // 8. SecondViewControllerに渡す文字列をセット
            selectedText = texts[indexPath.row]
    
            // 8. SecondViewControllerへ遷移するSegueを呼び出す
            performSegueWithIdentifier("showSecondView",sender: nil)
    
        }
    
        // Segueで遷移時の処理
        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
            if (segue.identifier == "showSecondView") {
                let secondVC: SecondViewController = (segue.destinationViewController as? SecondViewController)!
    
                // 11. SecondViewControllerのtextに選択した文字列を設定する
                secondVC.text = selectedText
            }
        }
    }
    
    SecondViewController
    import UIKit
    
    class SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
        @IBOutlet weak var tableView: UITableView!
    
        // 9. ViewControllerから受け取る文字列を入れる変数
        var text: String?
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            tableView.delegate = self
            tableView.dataSource = self
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 1
        }
    
        // セルのテキストを追加
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
    
            // 10. 受け取った文字列をセルに表示
            cell.textLabel?.text = text
            return cell
        }
    
        func tableView(table: UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath) {
            println(text)
        }
    }
    
    참조 소스
    https://github.com/senseiswift/tableviewtest/blob/master/tableviewtest/ViewController.swift
    계속해서 Swift에서 TableView modal 화면 숨기기
    http://qiita.com/senseiswift/items/90be98960b4c9dffe6aa

    좋은 웹페이지 즐겨찾기