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

関連記事

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

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

記事を読む

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

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

記事を読む

[Swift] CoreDataを使ってみる

SwiftでCoreData 今回はSwiftでCoreDataを使ってみます。 Xcodeプロ

記事を読む

[Swift] Asset Catalogについて

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

記事を読む

[iPhone App] WorkManagerの不具合について

バージョン1.2.2の不具合 11/25日現在、iPhoneアプリの「WorkManger」バージ

記事を読む

no image

爆走中!

挫折街道爆走中です! が! 迷走中でもあるわけです、はい。 そんな中で、迷走するに至った訳

記事を読む

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

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

記事を読む

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

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

記事を読む

[Objective-C] UINavigationControllerのセグエを使って画面遷移をする

画面遷移の実装 今回はUINavigationControllerを使って画面遷移をしたいと思いま

記事を読む

iPhoneApp WorkManager公開されました!

きっとリジェクトされるんだろうなと思いながら申請を出して1週間。 本日、公開のメールが届きまし

記事を読む

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