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

関連記事

NSDateを5分刻み・15分刻みで丸める

NsDateを初期値としてUIDatePicker等に設定する場合、日付の丸め処理が必要になるのでメ

記事を読む

no image

小さなことからコツコツと

これまでHTMLとCSSだけで生きてきただけあって、プログラム言語がいまいちわからない。 Mo

記事を読む

[iPhone App]WorkManager アップデートしました。

Processing For App Storeでやきもきした僕です。こんにちわ。 今

記事を読む

[Swift] CoreDataで検索したデータを削除する

CoreDataの検索と削除 前の投稿  CoreDataを使ってみる をふまえて今度は検索&削除

記事を読む

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

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

記事を読む

[Objective-C] 小数点を丸める!(数値変換)

小数点を含む数値 Objective-Cに限らず、小数点を含む数値を計算する場合はdoubleやf

記事を読む

Swiftの覚書1

Swift 先日、Appleから新言語Swiftが発表されました。 今までiOSアプ

記事を読む

[Swift] スクリーンサイズを取得する

スクリーンサイズを取得 iPhoneの幅と高さを取得する関数を紹介します。 iOS8から画面の傾

記事を読む

[Objective-C] デリゲート (Delegate)を理解する!

デリゲートとは? デリゲートに関しての意味合いとして、”委譲”とか”代理”と説明されることが多いで

記事を読む

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

plistからデータを取得してみます。 こちらのエントリーも参考にしてみてください。 プロパテ

記事を読む

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