Swiftで遊ぼう! on Hatena

あしたさぬきblogでやってた初心者オヤジのiOSプログラミング奮闘記がHatenaに来ました

Swiftで遊ぼう! - 469 - Navigation Controllers and Table Views 9

Swiftで遊ぼう!の古い記事-> Life-LOG OtherSide

Beginning iPhone Development with Swift: Exploring the iOS SDK

Beginning iPhone Development with Swift: Exploring the iOS SDK

RootViewControllerのデリゲーションメソッドをしていきます。

先ず、セクションが2つあるので、favoriteeが存在するかどうかでsectionを「1」返すか「2」返すか条件を入れて実装します。

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
 return favoritesList.favorites.isEmpty ? 1 : 2
}

シングルトン・インスタンスfavoritesListの唯一のプロパティfavoritesが空かどうか確認して、空なら「1」、空じゃ無ければ「2」を返します。

そして、rowの数を返す必須メソッドは次の通りです。

override func tableView(tableView: UITableView, 
    numberOfRowsInSection section: Int) -> Int {
  return section == 0 ? familyNames.count : 1
}

そしてセクションのタイトルを表示するメソッドも用意します。

override func tableView(tableView:  UITableView, 
        titleForHeaderInSection section: Int) -> String? {
   return section == 0 ? "All Font Familier" : "My Favorite Fonts"
}

そして最後に2つ目の必須メソッドの実装...

今日はここまで。