実現したいこと
質問をご覧いただきまして、ありがとうございます。
現在、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)
}
}
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
0
①
フッタを作成したくないセクションは、heightForFooterInSection
で0、viewForFooterInSection
でnilを返しましょう。
②
button.tag
にセクション番号を入れておいて、buttonTapped内で処理を分岐させるのがお手軽かと思います。
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.10%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2016/06/28 17:54
ご教示いただき、ありがとうございます。
2つの疑問ともすっきり解決いたしました。