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

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

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

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

Q&A

解決済

1回答

1365閲覧

テーブルビュー2段プロジェクト⇒08 sectionつきcellの選択による表示方法その2

Tomzy

総合スコア104

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

0グッド

0クリップ

投稿2016/09/23 03:07

###問題の概要

従来sectionによる区分けをしていなかった第1画面にsectionを加えたら、第2画面の表示にくるいが生じ前回07の質問に対する回答を頂き修正できました。

しかし、このsectionを加えることによるくるいはほかにも下記2カ所あることがわかりました。それは第1画面(View Controller)の selectedIndexPathInViewController.row を使っているからでした。

  1. 第2画面より第3画面に遷移するときの処理にくるいが生じ、呼んでくるウェブサイトのURLにくるいが生じました。→これはif文を使って成功裏に処理しました。
  2. 表示される第2画面(TableView02)の表示項目数にくるいが生じ、第2画面の表示項目が途中で切れてしまいました。→やはりif文を使ってselectedIndexPathInViewController+3.rowにする処理をしましたら、エラーがでて解決しません。

エラーの原因はif文をクラス内部で直接処理を書いている為で関数内部に書く必要があるようですが、うまく書けません。それが質問です。

第1画面のスクリーンショット
イメージ説明

第2画面の途中で切れた画面(本来なら "06.「よく使う項目」の登録"のあとに
"07.電話を受けた時の操作",
"08.呼び出し音の後の操作"]
が表示されるようにコーディングしてあります。

イメージ説明

###作成したコード

// TableView02.swift //省略 class TableView02: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet var tableView02: UITableView! var selectedIndexPathInViewController: NSIndexPath! let texts00 = ["01.電話番号で発信(動画)", "02「連絡先」の登録方法(動画)", "03「よく使う項目」の登録(動画)", //省略 override func viewDidLoad() { super.viewDidLoad() //省略 // テーブルビューのタイトルを表示 func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let label = UILabel(frame: CGRect(x:0, y:50, width: tableView.bounds.width, height: 50)) //省略 return label } //ここが問題の修正箇所→ エラーがでる if selectedIndexPathInViewController.section == 1 { func tableView(tableView: UITableView, numberOfRowsInSection section:Int) -> Int { return texts[selectedIndexPathInViewController.row].count } if selectedIndexPathInViewController.section == 2 { func tableView(tableView: UITableView, numberOfRowsInSection section:Int) -> Int { return texts[selectedIndexPathInViewController.row].count } } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { //省略 let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell //成功した第2画面を表示させる修正箇所 if selectedIndexPathInViewController.section == 1 { cell.label1.text = texts[selectedIndexPathInViewController.row][indexPath.row] cell.label2.text = subtexts[selectedIndexPathInViewController.row][indexPath.row] } if selectedIndexPathInViewController.section == 2 { cell.label1.text = texts[selectedIndexPathInViewController.row+3][indexPath.row] // cell.label1.text = texts[selectedIndexPathInViewController.row][indexPath.row+3] //cell.label2.text = subtexts[selectedIndexPathInViewController.row][indexPath.row+2] cell.label2.text = subtexts[selectedIndexPathInViewController.row+3][indexPath.row] } else{ } return cell } //省略 //テーブルビューのセルがタップされた処理 //成功した第3画面に遷移させる処理 var selectedIndexPath: NSIndexPath! override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { if (segue.identifier == "toDetailView") { print("ここまで来た03") let subVC02 = (segue.destinationViewController as? DetailViewController)! if selectedIndexPathInViewController.section == 1 { subVC02.urlString = textsURL[selectedIndexPathInViewController.row][selectedIndexPath.row] } if selectedIndexPathInViewController.section == 2 { subVC02.urlString = textsURL[selectedIndexPathInViewController.row+3][selectedIndexPath.row] } else{ }

###エラー内容

下記の2つのエラーが出ます。
0. 冒頭のclass TableView02: UIViewController, UITableViewDataSource, UITableViewDelegate { に、
イメージ説明
これは、テーブルビューの必要コードがないという意味と了解しています。
0. if selectedIndexPathInViewController.section == 1 { にExpected declaration というエラーがでます。これはif文をクラス内部で直接処理を書いている為で関数内部に書く必要があると了解しています。

###質問

このif文をどこにどのように書いたらよいのでしょうか。

よろしくお願いします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

swift

1 //ここが問題の修正箇所→ エラーがでる 2 if selectedIndexPathInViewController.section == 1 { 3 4 func tableView(tableView: UITableView, numberOfRowsInSection section:Int) -> Int { 5 return texts[selectedIndexPathInViewController.row].count 6 7 } 8 9 if selectedIndexPathInViewController.section == 2 { 10 11 func tableView(tableView: UITableView, numberOfRowsInSection section:Int) -> Int { 12 return texts[selectedIndexPathInViewController.row].count 13 14 } 15 16 }

のところを

swift

1func tableView(tableView: UITableView, numberOfRowsInSection section:Int) -> Int { 2 if selectedIndexPathInViewController.section == 1 { 3 return texts[selectedIndexPathInViewController.row].count 4 } 5 if selectedIndexPathInViewController.section == 2 { 6 return texts[selectedIndexPathInViewController.row+3].count 7 } 8 return 0 9}

とすればいいと思います。

投稿2016/09/23 09:35

TakeOne

総合スコア6299

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

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

Tomzy

2016/09/23 10:26

Take One さん ありがとうございました。うまくいきました。 最初、return texts[selectedIndexPathInViewController.row].count }の前にif文をいれる、こういう構成で試みたのですが、最後の } return 0 を入れていなかったため、うまくいかず、いろいろいじってしまいました。 まだ、まだわからないことが多いので、さらに質問をさせてもらいます。 よろしくお願いします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問