2017-11-18 6 views
0

私はUiTablewViewを使用するIOSアプリケーションを持っています。私のアプリは、以下の関数でクラッシュすることがよくあります。私はこのエラー( '致命的なエラー:範囲外のインデックス')をキャッチする方法を決定するための助けをしたい - インデックスが範囲外のときを決定する。私はこれをコード化する方法がわかりません。誰か助言できますか?致命的なエラー:インデックスが範囲外です - UITableViewController - このエラーを処理/捕捉する方法Swift

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: 
IndexPath) -> UITableViewCell { 
     let cell = UITableViewCell.init(style: .value1, reuseIdentifier: "menuCell") 
     cell.textLabel!.text = self.userFolderObjectArray[indexPath.section].arrayFolderNames[indexPath.row] // This is where I get the error 

     ... 
     return cell 

} 
+0

'numberOfSections'メソッドと' numberOfRowsInSection'メソッドはどのように設定されていますか? – erikmartens

+0

Swiftでランタイムエラー*を捕まえたり、トラップしたりすることはできません。あなたのアプリは単純に終了します。 )あなたがそれが遅すぎる前にこれを検出したい場合は*配列の境界を調べる必要があります;) –

+0

numberOfSectionsとuserFolderObjectArray [section] .arrayFolderNames.count numberOfRowsとしてuserFolderObjectArray.countを返した場合、私は私のuserFolderObjectArrayがリロードテーブルの処理中に他のスレッドによって変更される –

答えて

1

私は、配列の境界をチェックします:

if indexPath.section < self.userFolderObjectArray.count, 
    indexPath.row < self.userFolderObjectArray[indexPath.section].arrayFolderNames.count { 
    // do your stuff 
} else { 
    print("index error") 
} 

はあなたのnumberOfRowsnumberOfSectionsはどのようなものが見えますか?

+0

私は2つのセクションしか持っていません。 2番目のセクションは、任意の数の行を持つことができます。 – johnDoe

関連する問題