Swift カスタムテーブルセル上のアクションをデリゲート使って親クラスから実行してみる。

カスタムテーブルセルのアクセションをその親のビュークラスから操作する必要がでてくる事があります。 その時にデリゲート使って、他のクラスに処理を委任する方法を書いてみました。

キーとなるのはプロトコル、まずプロトコルでどのようなファンクションが用意されるのかを定義します。

protocol TouchCellDelegate {
    func getNo(id: Int) -> Void
}

カスタムセルクラスの中で、特定のタイミングでプロトコルで定義した処理を委任するコードを用意します。

func handleTap(gestureRecognizer: UIGestureRecognizer) {
        self.delegate?.getNo(self.id!)
    }

委任先のクラスでは、プロトコルTouchCellDelegateを委任する事を宣言します。

class MainView: UIViewController, UITableViewDelegate, UITableViewDataSource,TouchCellDelegate {

カスタムセルクラスの生成時に、カスタムセルが持つデリゲートの委任先が自身である事を定義します。これによって、カスタムセルクラスから、他のクラスへの委任設定が完了します。

cell.delegate = self

今回は、MainViewクラスがデリゲートの委任先としましたが、 デリゲートの委任関係を工夫する事により、クラス間の連携をより自由に行う事ができそうです。

全体サンプル gist.github.com

Swift 複数のカスタムセルを使いわけたいとき

Swift 複数のカスタムセルを使いわけたいときの実装方法。

具体的には、

①カスタスセルのclassを2種類作成する。

②tableView.registerClassでその2種類のカスタムセルを登録する。

③cellForRowAtIndexPath内で条件により使いわける。

でOKだった。

  override func viewDidLoad() {
        super.viewDidLoad()
      
        if let tableView = self.tableView {
            tableView.registerClass(TableViewCell.classForCoder(),forCellReuseIdentifier: "cell")
            tableView.registerClass(TableViewCell2.classForCoder(),forCellReuseIdentifier: "cell2")
            tableView.rowHeight = UITableViewAutomaticDimension
        }

    }
   override func tableView(tableView: UITableView, cellForRowAtIndexPath
        indexPath: NSIndexPath) -> UITableViewCell {
        
        switch indexPath.row{
        
        case 0:
            let cell = tableView.dequeueReusableCellWithIdentifier("cell2",forIndexPath: indexPath) as! TableViewCell2
            cell.selectionStyle = UITableViewCellSelectionStyle.None
            let cnt = msgs.count
            let m = msgs[cnt - indexPath.row - 1] as! Message
            cell.name.text = "name"
            cell.comment.text = m.text
            cell.comment.numberOfLines = 0
            cell.transform = self.tableView!.transform
            return cell
        default:
            let cell = tableView.dequeueReusableCellWithIdentifier("cell",forIndexPath: indexPath) as! TableViewCell
            cell.selectionStyle = UITableViewCellSelectionStyle.None
            let cnt = msgs.count
            let m = msgs[cnt - indexPath.row - 1] as! Message
            cell.name.text = m.name
            cell.comment.text = m.text
            cell.comment.numberOfLines = 0
            cell.transform = self.tableView!.transform
            return cell
        }
    }
    

PHP unixtimeを使った日付の変換フォーマット

PHP unixtimeを使った日付の変換フォーマット、 いつもググっているので、書いておく事にする。

現在のunixtimeを求める。

echo time();

unixtimeを指定の日付形式にフォーマット。

echo date('Y/m/d H:i:s', time());

指定日時からunixtimeを求める。

echo strtotime( '2012/04/18 15:55:53' );

Time Zoneの設定

date_default_timezone_set('Asia/Tokyo');

Go言語 Echoサーバーでテンプレートエンジンを試してみる。

ほぼサンプルどうり簡単につくってみる。

対象となるファイルを起動時に読み込む。

public/views/*.html

render時の引数で、そのファイル名を指定する。

return c.Render(http.StatusOK, "hello", "world")

全体サンプル gist.github.com

fasthttp - GoDoc

次回、pongo2使ってみよう。

github.com

Swift Erros

Swiftで開発しているとよく遭遇するエラーたち、、書き留めていこうと思う。

nib but the view outlet was not set

nibファイルの指定が間違えていた。nibNameの指定を修正。

  override func loadView() {
        if let view = UINib(nibName: "CommetForm", bundle: nil).instantiateWithOwner(self, options: nil).first as? UIView {
            self.view = view
        }
    }