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

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

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

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

Swift

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

Q&A

解決済

1回答

2800閲覧

SwiftでTableViewのリロード完了後に処理を実行したい

Atsushi_Kygo

総合スコア7

TableView

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

Swift

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

0グッド

0クリップ

投稿2020/10/15 07:41

編集2020/10/15 09:21

前提・実現したいこと

SwiftでTableViewをリロードした後に処理を実行したいのですが、「更新前と更新後でセルの数が違う」とエラーが出てしまいます。
最終的に、ボタンが押されたらセルの数を増やしていくという機能を作りたいです。

【イメージ画像】
![アプリの画面

発生している問題・エラーメッセージ

2020-10-15 16:13:58.381890+0900 TableViewReloadTest[6905:396777] *** Assertion failure in -[UITableView _Bug_Detected_In_Client_Of_UITableView_Invalid_Number_Of_Rows_In_Section:], UITableView.m:2432 2020-10-15 16:13:58.390914+0900 TableViewReloadTest[6905:396777] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' *** First throw call stack: ( 0 CoreFoundation 0x00000001084c2126 __exceptionPreprocess + 242 1 libobjc.A.dylib 0x0000000106bc5f78 objc_exception_throw + 48 2 CoreFoundation 0x00000001084c1f4f +[NSException raise:format:] + 0 3 Foundation 0x000000010667c1ca -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191 4 UIKitCore 0x000000010b24ab0c -[UITableView _Bug_Detected_In_Client_Of_UITableView_Invalid_Number_Of_Rows_In_Section:] + 115 5 UIKitCore 0x000000010b24a5a1 -[UITableView _endCellAnimationsWithContext:] + 14847 6 UIKitCore 0x000000010b264271 -[UITableView endUpdatesWithContext:] + 116 7 UIKitCore 0x000000010b264443 -[UITableView _performBatchUpdates:withContext:completion:] + 253 8 UIKitCore 0x000000010b264559 -[UITableView performBatchUpdates:completion:] + 97 9 TableViewReloadTest 0x00000001063a3487 $s19TableViewReloadTest0B10ControllerC12reloadButtonyyypF + 887 10 TableViewReloadTest 0x00000001063a3920 $s19TableViewReloadTest0B10ControllerC12reloadButtonyyypFTo + 80 11 UIKitCore 0x000000010b0560af -[UIApplication sendAction:to:from:forEvent:] + 83 12 UIKitCore 0x000000010a983584 -[UIControl sendAction:to:forEvent:] + 223 13 UIKitCore 0x000000010a9838a7 -[UIControl _sendActionsForEvents:withEvent:] + 332 14 UIKitCore 0x000000010a982190 -[UIControl touchesEnded:withEvent:] + 500 15 UIKitCore 0x000000010b0929ca -[UIWindow _sendTouchesForEvent:] + 1287 16 UIKitCore 0x000000010b094843 -[UIWindow sendEvent:] + 4774 17 UIKitCore 0x000000010b06e376 -[UIApplication sendEvent:] + 633 18 UIKitCore 0x000000010b0fe8d6 __processEventQueue + 13895 19 UIKitCore 0x000000010b0f526c __eventFetcherSourceCallback + 104 20 CoreFoundation 0x0000000108430845 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 21 CoreFoundation 0x000000010843073d __CFRunLoopDoSource0 + 180 22 CoreFoundation 0x000000010842fc1f __CFRunLoopDoSources0 + 248 23 CoreFoundation 0x000000010842a3f7 __CFRunLoopRun + 878 24 CoreFoundation 0x0000000108429b9e CFRunLoopRunSpecific + 567 25 GraphicsServices 0x0000000113647db3 GSEventRunModal + 139 26 UIKitCore 0x000000010b04faf3 -[UIApplication _run] + 912 27 UIKitCore 0x000000010b054a04 UIApplicationMain + 101 28 libswiftUIKit.dylib 0x000000010777e7b2 $s5UIKit17UIApplicationMainys5Int32VAD_SpySpys4Int8VGGSgSSSgAJtF + 98 29 TableViewReloadTest 0x00000001063a4eca $sSo21UIApplicationDelegateP5UIKitE4mainyyFZ + 122 30 TableViewReloadTest 0x00000001063a4e3e $s19TableViewReloadTest11AppDelegateC5$mainyyFZ + 46 31 TableViewReloadTest 0x00000001063a4f19 main + 41 32 libdyld.dylib 0x0000000108f2c415 start + 1 33 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' terminating with uncaught exception of type NSException CoreSimulator 732.17 - Device: iPhone 11 Pro (486C9A31-C400-4E1C-8817-5C0995EBA5AD) - Runtime: iOS 14.0 (18A372) - DeviceType: iPhone 11 Pro

該当のソースコード

Swift

1 2import UIKit 3 4class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 5 6 @IBOutlet weak var tableView: UITableView! 7 8 var cellNum = 3 // セルの数を指定 9 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 14 tableView.delegate = self 15 tableView.dataSource = self 16 } 17 18 19 /// ボタンが押された時 20 @IBAction func reloadButton(_ sender: Any) { 21 cellNum += 1 22 23 tableView.performBatchUpdates({ 24 self.tableView.reloadData() 25 }) { (finished) in 26 print("===== リロード完了後の処理 ======") 27 } 28 } 29 30 31 // セルの数 32 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 33 return cellNum 34 } 35 36 37 // セルの生成 38 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 39 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 40 cell.textLabel!.text = "sample text" 41 return cell 42 } 43 44 45}

試したこと

  1. tableView.performBatchUpdatesを使わず、tableView.reloadData()のみで実行する

-> 正常に動作するが、リロード完了後に実行という機能にできない
2. セルの数を増やすコード(cellNum += 1)を削除する
-> 正常に動作するが、セルの数が固定になる
3. tableView.beginUpdates() と tableView.endUpdates()を利用する
->同じエラーが発生する

補足情報(FW/ツールのバージョンなど)

Swift 5.3
Xcode 12.0.1

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

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

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

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

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

guest

回答1

0

自己解決

追記

tableViewでinsertRowsをするとエラーは発生せず、正常に動作しました。
恐らくtableViewのデータソースに反映せずに加算してリロードしようとしていたので、エラーになっていたのだと思います。

Swift

1 cellNum += 1 2 3 tableView.performBatchUpdates({ 4 self.tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .automatic) 5 self.tableView.reloadData()  6 }) { (finished) in 7 print("===== リロード完了後の処理 ======") 8 }

投稿2020/10/15 09:23

Atsushi_Kygo

総合スコア7

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問