質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Xcode 7

Xcode 7は、ソフトウェア開発のためのアップルの統合開発環境であるXcodeのバージョン。UIを作成するために用いるグラフィカルツールです。iOS9/OS X El Capitan/watchOS2に対応。Swift 2コンパイラーが搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

5304閲覧

tableViewに複数のsectionが存在する場合のfooterの使い方について

TsksHsgw

総合スコア23

Xcode 7

Xcode 7は、ソフトウェア開発のためのアップルの統合開発環境であるXcodeのバージョン。UIを作成するために用いるグラフィカルツールです。iOS9/OS X El Capitan/watchOS2に対応。Swift 2コンパイラーが搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2016/06/28 06:23

編集2016/06/28 06:27

実現したいこと
質問をご覧いただきまして、ありがとうございます。

現在、2つの疑問で悩んでいます。

①tableViewにfooterを追加する際、sectionが複数ある場合、
どちらか一方にのみfooterを追加する方法があるのかどうか。

②複数のsectionが存在する状況で、どちらにもfooterを実装した時、footer-Aとfooter-Bに別の機能を持たせる方法

上記の2つが解決できずに悩んでいます。

イメージ説明

現状ですと、画像のように、下のsectionのfooterをタップした場合でも上のsectionにセルが追加されてしまいます。

お詳しい方がいらっしゃいましたら、ご教示頂きたく存じます。
よろしくお願い致します。

下にソースコードを記します
###ソースコード
ViewController.swift

import UIKit class TableViewController: UITableViewController { var countriesinEurope = ["France", "Spain", "Germany"] var countriesinAsia = ["Japan","Korea","China"] override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 2 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int)-> Int { if section == 0 { return countriesinEurope.count }else if section == 1 { return countriesinAsia.count }else{ return 0 } } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)-> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell switch(indexPath.section){ case 0: cell.textLabel?.text = countriesinEurope[indexPath.row] case 1: cell.textLabel?.text = countriesinAsia[indexPath.row] default: cell.textLabel?.text = "" } return cell } override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let headerCell = tableView.dequeueReusableCellWithIdentifier("HeaderCell") as! CustomHeaderCell headerCell.backgroundColor = UIColor.cyanColor() headerCell.headerLabel.text = "Europe"; return headerCell } override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40)) footerView.backgroundColor = UIColor.blackColor() let button = UIButton(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 40)) button.setTitle("Button", forState: UIControlState.Normal) button.backgroundColor = UIColor.yellowColor() button.addTarget(self, action: "buttonTapped:", forControlEvents: UIControlEvents.TouchUpInside) footerView.addSubview(button) return footerView } func footerView(footerView: UIView?, forSection section: Int){ } override func tableView(tableView: UITableView, heightForFooterInSection section: Int)-> CGFloat { return 40.0 } func buttonTapped(sender: AnyObject){ let indexPath = NSIndexPath(forRow: 3, inSection: 0) countriesinEurope.append("") tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

フッタを作成したくないセクションは、heightForFooterInSectionで0、viewForFooterInSectionでnilを返しましょう。

button.tagにセクション番号を入れておいて、buttonTapped内で処理を分岐させるのがお手軽かと思います。

投稿2016/06/28 07:09

編集2016/06/28 07:28
fuzzball

総合スコア16731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

TsksHsgw

2016/06/28 08:54

fuzzballさん ご教示いただき、ありがとうございます。 2つの疑問ともすっきり解決いたしました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問