Swiftで遊ぼう! on Hatena

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

Swiftで遊ぼう! - 353 - カスタムTableViewでSectionをつくる パート3

Swiftで遊ぼう!の古い記事-> Life-LOG OtherSide
質問 : Swiftで遊ぼう! - 252 - FaceViewプロジェクト始動 - Swiftで遊ぼう! on Hatena

viewDidLoad()メソッドでnamesとkeysにデータが読み込まれました。

次にこのデータをテーブルに表示させるのが、デリゲーションメソッドです。

plainスタイルのTable Viewの場合は2つのデリゲーションメソッド(DataSource)の実装でいいのですが、Groupedスタイルの場合は、4つのメソッドを実装させる必要があります。

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  return keys.count
}
    
func tableView(tableView: UITableView,
       numberOfRowsInSection section: Int) -> Int {
  let key = keys[section]
  let nameSection = names[key]!
   return nameSection.count
}

func tableView(tableView: UITableView,
     titleForHeaderInSection section: Int) -> String? {
  return keys[section]
}
    
func tableView(tableView: UITableView,
  cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
 let cell = tableView.dequeueReusableCellWithIdentifier(sectionsTableIdentifier,
            forIndexPath: indexPath) as! UITableViewCell
  let key = keys[indexPath.section]
  let nameSection = names[key]!
  cell.textLabel!.text = nameSection[indexPath.row]
  return cell
}

これでランさせると次のようになる
f:id:yataiblue:20150618162911j:plain
今日はここまで。