RubyMotionSamples 確認 (3)
昨晩の続き。UITableViewController を継承している ViewController というクラスのソースを確認。
loadView メソド
UITableViewController の属性になってる tableView にオブジェクトを生成してセットしてます。その後、ViewController の属性に諸々を設定。
def loadView self.tableView = UITableView.new @rows = ['Swipe to the right to complete', 'Swipe to left to delete', 'Drag down to create a new cell', 'Pinch two rows apart to create cell', 'Long hold to start reorder cell'] @grabbedObject = nil @tableViewRecognizer = GestureRecognizer.alloc.initWithTableView(self.tableView, delegate:self) end
つうか self.tableView と @rows の関係が微妙にアレ。このあたりを云々してるのが
def tableView(tableView, cellForRowAtIndexPath: indexPath)
になるのかな、と見てるんですが本当かどうか。
他、このクラスでは callback っぽいメソドがもう二つ定義されているのですが、それぞれ属性の初期設定をしている模様。
def viewWillAppear(animated) navigationController.setNavigationBarHidden(true, animated:false) end def viewDidLoad tableView.backgroundColor = UIColor.blackColor tableView.separatorStyle = UITableViewCellSeparatorStyleNone tableView.rowHeight = NormalCellFinishingHeight end
navigationController という変数ですが、iOS (iPhone/iPad) アプリ開発で UITableView と UINavigationView を組合せて選択式メニューのような機能を作る というエントリによると_UITableViewController 側から、自分を管理している UINavigationViewController のインスタンスを参照するときは self.navigationController を使う_という記述があります。成程。
以下、確認しておく必要がありそう。
- def tableView(tableView, numberOfRowsInSection: section)
- def tableView(tableView, heightForRowAtIndexPath: indexPath)
- def tableView(tableView, cellForRowAtIndexPath: indexPath)
上記、継続して確認の方向。これらの確認が終わると次は gestureRecognizer 方面となります。
- def gestureRecognizer(gestureRecognizer, needsAddRowAtIndexPath: indexPath)
- def gestureRecognizer(gestureRecognizer, needsCommitRowAtIndexPath: indexPath)
- def gestureRecognizer(gestureRecognizer, needsDiscardRowAtIndexPath: indexPath)
- def gestureRecognizer(gestureRecognizer, didEnterEditingState: state, forRowAtIndexPath: indexPath)
- def gestureRecognizer(gestureRecognizer, canEditRowAtIndexPath: indexPath)
- def gestureRecognizer(gestureRecognizer, canMoveRowAtIndexPath: indexPath)
- def gestureRecognizer(gestureRecognizer, needsCreatePlaceholderForRowAtIndexPath: indexPath)
- def gestureRecognizer(gestureRecognizer, needsMoveRowAtIndexPath: sourceIndexPath, toIndexPath: destinationIndexPath)
- def gestureRecognizer(gestureRecognizer, needsReplacePlaceholderForRowAtIndexPath: indexPath)
あと、いっちゃん最後に以下なメソドが定義されてます。
- def backgroundColorForIndexPath(indexPath)
これはこれで謎。
確認着手
とりあえず tableView 方面から。
def tableView(tableView, numberOfRowsInSection: section)
Customize the number of rows in the table view. とあります。実装としては以下なので、セクションを問わず (というかこの実装の場合、セクションというものが無いはず)、@row の要素の数で固定、という形なのか。
def tableView(tableView, numberOfRowsInSection: section) @rows.count end
def tableView(tableView, heightForRowAtIndexPath: indexPath)
セルの高さを設定するメソドな模様。
def tableView(tableView, heightForRowAtIndexPath: indexPath) NormalCellFinishingHeight end
ちなみに NormalCellFinishingHeight はクラス定義の先頭で定義されてました。
NormalCellFinishingHeight = 60
def tableView(tableView, cellForRowAtIndexPath: indexPath)
tableView 内の特定の位置にセルを挿入、とありますね。現在表示されている行についてのみが呼び出されるとのこと。成程。このメソドは少々長いようなのでぼちぼち確認って事で確認の度に追記な方向。
追記
tableView(tableView, cellForRowAtIndexPath: indexPath) について。
手続きの先頭あたりで対象となる行を取り出したり色を取り出したりしてます。
object = @rows[indexPath.row] backgroundColor = backgroundColorForIndexPath(indexPath)
で、以下な条件分岐。
- object が 'Continue...' の場合
- indexPath.row が 0 の場合
- indexPath.row が 0 以外
- object が 'Continue...' 以外
とりあえず 'Continue...' は gestureRecognizer(gestureRecognizer, needsAddRowAtIndexPath: indexPath) にて定義されているのは分かりましたがその先は別途で。GestureRecognizer が関係してくるはず。
最後の object が 'Continue...' 以外の部分が通常ケイス、ということで
- セルなオブジェクトを取得
- 色を決めて
- cell な属性に設定
ということをやってる風に見えます。気になるのは UITableView#dequeueReusableCellWithIdentifier (長い) というソレ。名前からなんとなく類推できるんですが、表示されなくなったセルを再利用して再度割当て、というナニなのかどうか。