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

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

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

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

Q&A

解決済

1回答

276閲覧

TableViewCellをセクションに分けて表示したい

KCROW

総合スコア7

Swift

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

0グッド

0クリップ

投稿2021/01/03 02:36

筋トレアプリを作成中で、TableViewCellをセクションに分けて表示したく下記を参考にしたのですが
「Thread 1: Fatal error: Index out of range」とエラーが出てしまいます。

<参考>
https://qiita.com/BMJr/items/ca7bcf76d36acbdef75e

swift

1import UIKit 2 3class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { 4 5 func numberOfSections(in tableView: UITableView) -> Int { 6 return mySections.count 7 } 8 9 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 10 return twoDimArray[section].count 11 } 12 13 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 14 return mySections[section] 15 } 16 17 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 18 let cell = tableView.dequeueReusableCell(withIdentifier: "SampleCell", for: indexPath) 19 cell.textLabel?.text = twoDimArray[indexPath.section][indexPath.row] 20 return cell 21 } 22 23 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 24 selectedBui = mySections[indexPath.section] 25 selectedSyumoku = twoDimArray[indexPath.section][indexPath.row] 26 // Segueを使った画面遷移を行う関数 27 performSegue(withIdentifier: "Segue", sender: nil) 28 } 29 30 31 32 33 34 var mySections = [String]() 35 var twoDimArray = [[String]]() 36 var selectedBui = "" 37 var selectedSyumoku = "" 38 39 40 @IBOutlet weak var tableView: UITableView! 41 42 43 44 override func viewDidLoad() { 45 super.viewDidLoad() 46 47 mySections = ["肩","胸","背中","腕","脚","腹"] 48 49 for _ in 0 ... 5 { 50 twoDimArray.append([]) 51 52 twoDimArray[0] = ["ショルダープレス","サイドレイズ"] 53 twoDimArray[1] = ["ダンベルプレス","ダンベルフライ"] 54 twoDimArray[2] = ["オーバーロー","ラットプル"] 55 twoDimArray[3] = ["ハンマーカール","スカルクラッシャー"] 56 twoDimArray[4] = ["レッグプレス","レッグエクステンション"] 57 twoDimArray[5] = ["レッグレイズ"] 58 59 } 60 61 62 63 64 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 65 return true 66 } 67 68 //スワイプしたセルを削除 ※arrayNameは変数名に変更してください 69 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 70 if editingStyle == UITableViewCell.EditingStyle.delete { 71 twoDimArray.remove(at: indexPath.row) 72 73 tableView.deleteRows(at: [indexPath as IndexPath], with: UITableView.RowAnimation.automatic) 74 } 75 76} 77} 78 79}

こちらのサイト(https://kichie-com.hatenablog.com/entry/2016/09/25/052547)を参考に修正したものの、変わらずエラーが出てしまいます。

ヒントでも良いので、どなたかよろしくお願いいたします。

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

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

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

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

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

hoshi-takanori

2021/01/03 03:50

インデントが崩れててよく分かりませんが、viewDidLoad の for 文の閉じかっこ } が足りない気がします。
KCROW

2021/01/03 04:24

コメントありがとうございます!おっしゃる通り、閉じ}が足りない初歩的なミスでした… 無事エラー消えましたありがとうございます。
guest

回答1

0

自己解決

for文の閉じが抜けていたため、追加。

投稿2021/01/03 04:26

KCROW

総合スコア7

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問