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

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

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

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

Q&A

解決済

1回答

1749閲覧

UICollectionViewを実装したアプリを起動すると落ちる

hodoru3sei

総合スコア284

Swift

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

0グッド

0クリップ

投稿2018/09/02 09:08

Swiftプログラミング入門というほんでSwiftについて勉強しているのですが、UICollectionViewを実装する単元を写経したら落ちてしまいます。
教本のサンプルコードを全コピーして貼り付けても落ちてしまいました。配布されているアプリは動いたのに私の作ったものはなぜ動かないのかわからないです。
ストーリーボードを設定している部分はAtributes inspectorのIdentifierにreuseIdentifierと入力しただけです。この部分もサンプルプログラムからコピーして貼り付けたのですがやはり変化なく起動時に落ちてしまいます

lang

12018-09-02 18:05:26.069069+0900 collectionView[45451:2443478] *** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3698.54.4/UICollectionView.m:5283 22018-09-02 18:05:26.074040+0900 collectionView[45451:2443478] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier reuseIdentifire - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' 3*** First throw call stack: 4( 5 0 CoreFoundation 0x00000001133b61e6 __exceptionPreprocess + 294 6 1 libobjc.A.dylib 0x000000010f32f031 objc_exception_throw + 48 7 2 CoreFoundation 0x00000001133bb472 +[NSException raise:format:arguments:] + 98 8 3 Foundation 0x000000010edd2652 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193 9 4 UIKit 0x00000001106a4cc0 -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] + 2475 10 5 UIKit 0x00000001106a4f4e -[UICollectionView dequeueReusableCellWithReuseIdentifier:forIndexPath:] + 169 11 6 collectionView 0x000000010ea1e497 _T014collectionView0B10ControllerCAASo012UICollectionB4CellCSo0dB0C_10Foundation9IndexPathV13cellForItemAttF + 375 12 7 collectionView 0x000000010ea1e8bc _T014collectionView0B10ControllerCAASo012UICollectionB4CellCSo0dB0C_10Foundation9IndexPathV13cellForItemAttFTo + 92 13 8 UIKit 0x000000011068e756 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 295 14 9 UIKit 0x000000011068e629 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 35 15 10 UIKit 0x00000001106938ae -[UICollectionView _updateVisibleCellsNow:] + 4505 16 11 UIKit 0x0000000110699688 -[UICollectionView layoutSubviews] + 364 17 12 UIKit 0x000000010fc9a7a8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1515 18 13 QuartzCore 0x0000000116749456 -[CALayer layoutSublayers] + 177 19 14 QuartzCore 0x000000011674d667 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 395 20 15 QuartzCore 0x00000001166d40fb _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 343 21 16 QuartzCore 0x000000011670179c _ZN2CA11Transaction6commitEv + 568 22 17 UIKit 0x000000010fbe5269 __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 141 23 18 CoreFoundation 0x0000000113358b0c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12 24 19 CoreFoundation 0x000000011333d2db __CFRunLoopDoBlocks + 331 25 20 CoreFoundation 0x000000011333ca84 __CFRunLoopRun + 1284 26 21 CoreFoundation 0x000000011333c30b CFRunLoopRunSpecific + 635 27 22 GraphicsServices 0x00000001158d2a73 GSEventRunModal + 62 28 23 UIKit 0x000000010fbcb057 UIApplicationMain + 159 29 24 collectionView 0x000000010ea20697 main + 55 30 25 libdyld.dylib 0x00000001141c9955 start + 1 31 26 ??? 0x0000000000000001 0x0 + 1 32) 33libc++abi.dylib: terminating with uncaught exception of type NSException 34(lldb)

lang

1import UIKit 2 3class ViewController: UIViewController { 4 let items: [CGFloat] = [ 5 1.0, 6 2.0, 7 3.0, 8 4.0, 9 5.0, 10 6.0, 11 7.0, 12 8.0, 13 9.0 14 ] 15 let reuseIdentifier = "reuseIdentifire" 16 17 @IBOutlet weak var collectionView: UICollectionView! 18 override func viewDidLoad() { 19 20 21 super.viewDidLoad() 22 // Do any additional setup after loading the view, typically from a nib. 23 collectionView.dataSource = self 24 collectionView.delegate = self 25 collectionView.allowsSelection = true 26 } 27 28 override func didReceiveMemoryWarning() { 29 super.didReceiveMemoryWarning() 30 // Dispose of any resources that ca 31 collectionView.dataSource = self 32 collectionView.delegate = self 33 } 34 35} 36extension ViewController: UICollectionViewDataSource { 37 38 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int)->Int{ 39 40 return items.count 41 } 42 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 43 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) 44 cell.contentView.layer.borderColor = UIColor.black.cgColor 45 cell.contentView.layer.borderWidth = items[indexPath.row] 46 47 let bgView = UIView() 48 bgView.backgroundColor = .lightGray 49 cell.selectedBackgroundView = bgView 50 51 return cell 52 } 53} 54extension ViewController: UICollectionViewDelegate { 55 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 56 let alertController = UIAlertController(title: nil, message: "ボーダーのの太さは(items[indexPath.row])です", preferredStyle: .alert) 57 alertController.addAction(UIAlertAction(title: "ok", style: .default, handler: nil)) 58 present(alertController, animated: true, completion: nil) 59 } 60} 61 62

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

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

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

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

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

guest

回答1

0

ベストアンサー

この辺を見てあげてください。

swift

1// よく見て違うよ。 fierとfire。 2let reuseIdentifier = "reuseIdentifire"

投稿2018/09/02 10:03

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

hodoru3sei

2018/09/02 10:22

ありがとうございます。 実行時エラーになっちゃうんですね全然気づかなかったです
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問