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

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

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

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

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

Swift

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

Q&A

解決済

1回答

1878閲覧

tableViewで1行を削除する方法

AppDvl

総合スコア58

TableView

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

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

Swift

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

0グッド

0クリップ

投稿2021/10/30 03:05

#わからないこと
tableViewの1行をスワイプ削除するところでエラーがになる。
自分では、どうしても解決できないのでご教授お願いしたい。
イメージ説明

#考察
1,エラーの部分をコメントアウトするとエラーが消えてビルドできるが、スワイプで削除するとアプリが落ちる
2,誤字等ではなく、何かしら辻褄が合わないことが発生しているらしいが原因がわからない。
3,久しぶりにXcodeを開いたらエラーがこの発生した、以前は起きてなかったような気が・・・

#エラーの内容

Reference to member 'deleteRows' cannot be resolved without a contextual type

#コード全文

swift

1import UIKit 2 3class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { 4 let tableView = UITableView() 5 var ovalTuple:[(arrayName:String,No:String)] = [] 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 10 11 tableView.backgroundColor = .white 12 tableView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height:self.view.bounds.height )//ナビゲーションバーの高さnavBarHeightに合わせるnavBarHeight 13 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "data") //register登録 14 tableView.dataSource = self 15 tableView.delegate = self 16 17 //ovalTupleに値を追加 18 let aaa = (arrayName:"まる",No:"maru") 19 ovalTuple.append(aaa) 20 ovalTuple.append(aaa) 21 ovalTuple.append(aaa) 22 self.view.addSubview(tableView) 23 } 24} 25 26 27extension ViewController { 28 //MARK:- テーブル関数 29 // セルの行数を指定 30 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 31 return ovalTuple.count 32 } 33 // sectionの数を決める 34 func numberOfSections(in tableView: UITableView) -> Int { 35 return 1 36 } 37 // Cellの高さを決める 38 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 39 return 30 40 } 41 42 //セルの値を設定、一つのsectionの中に入れるCellの内容を決める。 43 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 44 let cell = tableView.dequeueReusableCell(withIdentifier: "data", for: indexPath) as UITableViewCell 45 cell.textLabel?.text = String(ovalTuple[indexPath.row].arrayName) 46 cell.textLabel?.textColor = .black 47 cell.backgroundColor = .white 48 //タップすると指定色にする 49 let selectionView = UIView() 50 selectionView.backgroundColor = .lightGray 51 cell.selectedBackgroundView = selectionView 52 return cell 53 } 54 55 // セルを選択したときのアクション 56 func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) { 57 ///キーボードを閉じる 58 self.view.endEditing(true) 59 } 60 61 //セルの編集許可 62 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool 63 { 64 return true 65 } 66 67 //スワイプしたセルを削除 68 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 69 if editingStyle == UITableViewCell.EditingStyle.delete { 70 ovalTuple.remove(at: indexPath.row)//配列からrow番号の文字を消す 71 tableView.deleteRows(at: [indexPath as IndexPath], with: UITableView.RowAnimation.automatic)//ここでエラー 72 } 73 74 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 75 // ヘッダーViewの高さを返す 76 return 50 77 } 78 79 func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 80 return "test" 81 } 82 83 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 84 let headerView:UIView = UIView() 85 headerView.backgroundColor = UIColor.white 86 return headerView 87 } 88 } 89} 90

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

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

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

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

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

guest

回答1

0

ベストアンサー

swift

1 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 2 if editingStyle == UITableViewCell.EditingStyle.delete { 3 ovalTuple.remove(at: indexPath.row)//配列からrow番号の文字を消す 4 tableView.deleteRows(at: [indexPath as IndexPath], with: UITableView.RowAnimation.automatic)//ここでエラー 5 }

上記のメソッドを閉じる } が漏れていて、
後続のfunc tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {が前述のメソッドの内部関数として定義されてしまい、エラー発生行の tableView が、この内部関数の tableView のことだと認識されてエラーになっているようです。

メソッドを閉じる } を正しく入れればエラーが解消されると思います。

投稿2021/10/30 04:50

TakeOne

総合スコア6299

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

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

AppDvl

2021/10/31 00:32

TakeOneさん ご指摘とおりにすると上手くいきまいた。 }が原因とは。なかなか初心者から抜け出せません。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問