LoginSignup
8
8

More than 5 years have passed since last update.

SwiftでのTableView modal画面の非表示

Last updated at Posted at 2015-09-01

これまでの記事の続きになります。
データ表示
http://qiita.com/senseiswift/items/9b5476531a843b0e314a
画面遷移
http://qiita.com/senseiswift/items/20d09c523772caaf8005

Swiftでの簡単なテーブルビュー modal画面の非表示

やること

  1. SecondViewControllerにmodal(自分自身)の非表示処理を追加
  2. ViewControllerに選択状態を解除するIndexPathを入れる変数を追加
  3. セル選択時に、選択したIndexPathをセット
  4. 画面が表示される時に呼ばれるメソッド(viewWillAppear)を追加
  5. selectedRowがセットされていたらテーブルビューの選択を解除する

SecondViewに遷移して、一番上のセルをタップするとSecondViewが閉じて、
ViewControllerに戻ります。(ViewControllerのテーブルの選択状態も解除されます)

ViewController
import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!

    // SecondViewに渡す文字列
    var selectedText: String?

    // 2. 選択状態を解除するIndexPathを入れる変数
    var selectedRow: NSIndexPath?

    // テーブルに表示するテキスト
    let texts = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
    }

    // 4. 画面が表示される時に呼ばれるメソッドを追加
    override func viewWillAppear(animated: Bool) {
        super.viewDidDisappear(animated)

        // 5. selectedRowがセットされていたら選択を解除する
        if (selectedRow != nil)
        {
            tableView?.deselectRowAtIndexPath(selectedRow!, animated: false)
        }
    }

    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])

        // SecondViewControllerに渡す文字列をセット
        selectedText = texts[indexPath.row]

        // SecondViewControllerへ遷移するSegueを呼び出す
        performSegueWithIdentifier("showSecondView",sender: nil)

        // 3. 選択状態を解除するIndexPathをセット
        selectedRow = indexPath
    }

    // Segueで遷移時の処理
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if (segue.identifier == "showSecondView") {
            let secondVC: SecondViewController = (segue.destinationViewController as? SecondViewController)!

            // SecondViewControllerのtextに選択した文字列を設定する
            secondVC.text = selectedText
        }
    }
}
SecondViewController
//
//  SecondViewController.swift
//  tableviewtest
//
//  Created by sensei on 2015/08/31.
//  Copyright (c) 2015年 senseiswift. All rights reserved.
//

import UIKit

class SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!

    // 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")

        // 受け取った文字列をセルに表示
        cell.textLabel?.text = text
        return cell
    }

    func tableView(table: UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath) {
        // 1. セル選択時にmodal(自分自身)の非表示処理を追加
        self.dismissViewControllerAnimated(true, completion: nil)
    }
}

参考ソース
https://github.com/senseiswift/tableviewtest/commit/6ce22f2157d389100efc88f592901309b4975c4b

次回はSecondViewでセルタップ時にSafariを起動するようにしました。
http://qiita.com/senseiswift/items/70825c8dd4b8dd9d73f9

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8