Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

[Swift] UITableViewのセルにアクションを設定する

公開日: : iPhone App 開発, Swift


Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

セル選択→アクション

UITableViewControllerを使用したテーブルビューの画面があるとします。
そしてUITableViewのセルを選択したときにアクションを実行させるのですが、スマートな方法を見つけたので紹介します。

言語はSwiftになります。

処理の流れは。。。

  1. セル列に個別のアクション(メソッド)を用意する
  2. アクションマップ(配列)を設定する
  3. セル選択時にアクションマップからアクションを実行させる

 

画面はこんな感じ。
iOS Simulator Screen Shot 2014.08.27 17.16.19

ソースファイルはTableViewActionController.swiftで作成しました。
以下、ソースコードです。

import UIKit

class TableViewActionController : UITableViewController {
    
    var actionMap: [[Void -> Void]] {
        return [
            // Section1
            [
                self.cell1_1,
                self.cell1_2,
                self.cell1_3
            ],
            // Section2
            [
                self.cell2_1,
                self.cell2_2
            ]
        ]
    }
    
    func cell1_1() {
        println("cell1_1");
    }
    
    func cell1_2() {
        println("cell1_2");
    }
    
    func cell1_3() {
        println("cell1_3");
    }
    
    func cell2_1() {
        println("cell2_1");
    }
    
    func cell2_2() {
        println("cell2_2");
    }
    
    // MARK: UITableViewDelegate
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let action = actionMap[indexPath.section][indexPath.row]
        
        action()
        
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
}
  • 5行目:actionMap変数で定義したメソッドを設定しています。
  • 21〜39行目:各セル選択時に実行されるメソッドです。
  • 43行目:UITableViewのDelegateメソッドであるdidSelectRowAtIndexPath(セル選択時に呼ばれる)にactionMap変数の配列に指定されたアクションを実行します。
  • 48行目:deselectRowAtIndexPathで選択状態のセルを解除します。

まとめ

普通ならdidSelectRowAtIndexPathでif文を使い[セクション・列]を判断しメソッドを実行させるのですが、アクションをまとめるやり方は、とてもスマートに記述できたのが分かります。配列にメソッドを設定しているのがキモですね。

Objective-cではこんな設定できるのかわかりませんが、Swift素晴らしい!と思いました。

ad

関連記事

no image

一旦、離脱中!

サイト制作の仕事がちょろちょろちょろっと入ってきたので、寂しいけれどしばしXcodeとはお別れ。

記事を読む

[Swift] UITabBarControllerでタブ選択時のイベントを取得する

UITabBarController タブを使った画面を作成する場合はUITabBarContro

記事を読む

no image

OSSの使い方がわからない・・・。

チュートリアル系の、storyboradでview controllerを置いて画面遷移を設定してっ

記事を読む

備忘録的メモ

Xcode5にアップデートして、とりあえずアプリを走らせてみたらナビゲーションバーがかぶってる・・・

記事を読む

[Objective-C] 共通関数をまとめたクラスを作ろう!

共通クラスとは? アプリを作っていると「この処理何回も書いてるなー」ということが多々あると思います

記事を読む

[Swift] CoreDataのテーブルからデータ件数を取得する

件数を取得する fetchResultsControllerなどを使わず、直接テーブルのデータ件数

記事を読む

[Objective-C] 文字列操作の便利Tips

Tipsいろいろ 先日の投稿 共通関数をまとめたクラスを作ろう!の流れで、文字列を操作するときに便

記事を読む

[Swift] 定数クラスをつくってみる

Objective-cの場合 今回は共通にする定数をまとめるクラスを作ってみます。 まずはO

記事を読む

[Objective-C] バージョン更新時に変更内容をアラートで出力する

アプリの更新内容を表示したい! バージョン更新時に変更内容を表示させたいのは、ユーザーがいちいちA

記事を読む

[Swift] Swiftでいろんなfor文まとめてみた

いろんなfor文 プログラム作成で必ず出てくるfor文を自分なりにまとめてみました。 Swift

記事を読む

ad

Message

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

ad

[PHP] curl転送してみる(googleに)

curlでgoogle画像検索APIにアクセスしてみます。 cu

[PHP] PDOでMySQLの接続確認をする

PDO PHPでMySQLに接続する際には、mysql_connec

[PHP] ランダムな英数字を生成する

便利系メソッド 今回はPHPでランダムな英数字を作成してみます。

[Swift] プロパティリスト(plist)の値を取得

plistからデータを取得してみます。 こちらのエントリーも参考にし

[Swift] Asset Catalogについて

XCode5から追加されたAsset Catalog。 いままで標準

→もっと見る

  • 1978年の七夕生まれ。 25才でweb業界の門を叩き、28才でフリーランスに。 現在は、フリーランスでマークアップ中心に、wordpressのカスタマイズやデザインをしております。 また、iPhoneアプリの開発もしております。

Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524
PAGE TOP ↑

Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524