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

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

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

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

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

Q&A

解決済

1回答

3916閲覧

UITableViewを使って開閉式のmenuを作成したいのですが、、、

Yukihiro.Y

総合スコア13

Swift

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

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

0グッド

1クリップ

投稿2016/09/20 08:49

現在、UITableViewを使ってヘッダをタップして開閉式のmenuを作成しようとしているのですが、どうにもわかりません。

セクションのviewにタップジェスチャーを追加して、タップイベントで対象のセクションをreloadSections()でcellの数を読み込ませて作成する・・・といったことをしたいです。しかし一番最初のタップはうまく作動して、想定通りのcell数を作成して表示するのですが、二度目に他のヘッダをタップすると落ちてしまいます。一度目と同じヘッダならば落ちることはないのですが・・・

現状のコードを載せます。

Xcode7.31
swift

Swift

1 2import UIKit 3 4class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource { 5 6 var tableView = UITableView() 7 8 var selectedIndex:Int? = nil 9 10 //セクションのタイトルと、cellに表示したい内容をtapleで用意 11 let contents = [("A",["1","2","3"]),("B",["1","2","3","4","5","6","7","8"]),("C",["1","2"]),("D",["1","2","3"]),("E",["1","2","3","4"]),("F",["1","2"])] 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 16 } 17 18 override func viewWillAppear(animated: Bool) { 19 20 tableView.frame = CGRectMake(0,0,200,500) 21 tableView.dataSource = self 22 tableView.delegate = self 23 24 self.view.addSubview(tableView) 25 } 26 27 func numberOfSectionsInTableView(tableView: UITableView) -> Int { 28 29 return contents.count 30 } 31 32 33 34 //セクションの高さを指定 35 func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 36 return 40 37 } 38 39 //セクションの編集 40 func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 41 42 let sectionView = UIView(frame:CGRectMake(0,0,self.view.frame.width,40)) 43 sectionView.backgroundColor = UIColor.whiteColor() 44 sectionView.tag = section 45 46 //セクションにタイトル設定 47 let sectionLabel = UILabel(frame:CGRectMake(10,20,self.view.frame.width,20)) 48 sectionLabel.text = contents[section].0 49 50 //セクションにタップジェスチャーを追加 51 sectionView.addGestureRecognizer(UITapGestureRecognizer(target: self,action:#selector(sectionTapAction(_:)))) 52 53 sectionView.addSubview(sectionLabel) 54 55 return sectionView 56 } 57 58 59 60 //セルの個数を指定する 61 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 62 63 if selectedIndex == nil{ 64 return 0 65 } 66 else{ 67 68 if selectedIndex! == section{ 69 70 return self.contents[section].1.count 71 72 }else{ 73 return 0 74 } 75 76 } 77 } 78 79 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 80 81 let cell: UITableViewCell = UITableViewCell() 82 cell.frame = CGRectMake(0,0,200,50) 83 84 let label = UILabel(frame:CGRectMake(0,0,100,40)) 85 label.text = contents[selectedIndex!].1[indexPath.row] 86 87 return cell 88 } 89 90 91 92 93 func sectionTapAction(sender:UITapGestureRecognizer){ 94 95 96 self.selectedIndex = sender.view!.tag 97 98 self.tableView.reloadSections(NSIndexSet(index: self.selectedIndex!), withRowAnimation: .None) 99 100 101 } 102 103 104 override func didReceiveMemoryWarning() { 105 super.didReceiveMemoryWarning() 106 // Dispose of any resources that can be recreated. 107 } 108 109} 110

自分なりにいろいろ調べてみたのでが、何が問題なのかわかりませんでした。拙い文章で質問の意図が伝わるか不安ですが、お知恵を拝借できれば幸いです。

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

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

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

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

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

guest

回答1

0

ベストアンサー

セルが開いているかどうかの値(Bool)をデータに持たせると以下のようにやりたいことができると思います。

swift

1import UIKit 2 3class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource { 4 5 var tableView = UITableView() 6 7 var selectedIndex:Int? = nil 8 9 //セクションのタイトルと、cellに表示したい内容をtapleで用意 10 var contents = [("A",["1","2","3"], false),("B",["1","2","3","4","5","6","7","8"], false),("C",["1","2"], false),("D",["1","2","3"], false),("E",["1","2","3","4"], false),("F",["1","2"], false)] 11 12 override func viewDidLoad() { 13 super.viewDidLoad() 14 tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell") 15 } 16 17 override func viewWillAppear(animated: Bool) { 18 19 tableView.frame = CGRectMake(0,0,200,500) 20 tableView.dataSource = self 21 tableView.delegate = self 22 self.view.addSubview(tableView) 23 } 24 25 func numberOfSectionsInTableView(tableView: UITableView) -> Int { 26 return contents.count 27 } 28 29 //セクションの高さを指定 30 func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 31 return 40 32 } 33 34 //セクションの編集 35 func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 36 37 let sectionView = UIView(frame:CGRectMake(0,0,self.view.frame.width,40)) 38 sectionView.backgroundColor = UIColor.whiteColor() 39 sectionView.tag = section 40 41 //セクションにタイトル設定 42 let sectionLabel = UILabel(frame:CGRectMake(10,20,self.view.frame.width,20)) 43 sectionLabel.text = contents[section].0 44 45 //セクションにタップジェスチャーを追加 46 sectionView.addGestureRecognizer(UITapGestureRecognizer(target: self,action:#selector(sectionTapAction(_:)))) 47 sectionView.addSubview(sectionLabel) 48 return sectionView 49 } 50 51 52 // セルの個数を指定する 53 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 54 return contents[section].2 ? contents[section].1.count : 0 55 } 56 57 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 58 59 let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) 60 cell.textLabel?.text = contents[indexPath.section].1[indexPath.row] 61 return cell 62 } 63 64 func sectionTapAction(sender:UITapGestureRecognizer){ 65 self.selectedIndex = sender.view!.tag 66 contents[selectedIndex!].2 = !contents[selectedIndex!].2 67 self.tableView.reloadSections(NSIndexSet(index: self.selectedIndex!), withRowAnimation: .None) 68 } 69 70 override func didReceiveMemoryWarning() { 71 super.didReceiveMemoryWarning() 72 } 73}

投稿2016/09/20 11:27

_Kentarou

総合スコア8490

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

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

Yukihiro.Y

2016/09/20 13:05

とても素晴らしい回答ありがとうございます。同時に勉強不足を痛感しました。 おかげ様で先に進めそうです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問