swift uicollectionviewcell reuseIdentifier
自己去继承了UICollectionViewCell
然后内部加了自己的自动约束
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { gLog.verbose(“collectionView=\(collectionView), indexPath=\(indexPath)”) //let cell = collectionView.dequeueReusableCellWithReuseIdentifier(OptionCollectionViewCellId, forIndexPath: indexPath) let cell = OptionCollectionViewCell.init(curOption: self.optionList[indexPath.row] , reuseIdentifier: OptionCollectionViewCellId) // var textColor:UIColor = UIColor() // if cell.selected { // cell.backgroundColor = OptionCellBackgroundColorSelected // textColor = UIColor.whiteColor() // } else { // cell.backgroundColor = OptionCellBackgroundColorNotSelected // textColor = CommonTextColorBlack // } // // cell.layer.cornerRadius = 12 // cell.layer.borderWidth = 1 // cell.layer.borderColor = UIColor.clearColor().CGColor // // if indexPath.row < self.optionList.count { // let curOption = self.optionList[indexPath.row] // let curOptionLabel = UILabel() // curOptionLabel.text = curOption // curOptionLabel.font = OptionCellItemLabelFont // curOptionLabel.textAlignment = .Center // curOptionLabel.textColor = textColor // cell.contentView.addSubview(curOptionLabel) // constrain(curOptionLabel) {curOptionLabel in //// curOptionLabel.top == curOptionLabel.superview!.top + 5 //// curOptionLabel.left == curOptionLabel.superview!.left + 5 //// curOptionLabel.center == curOptionLabel.superview!.center //// curOptionLabel.right == curOptionLabel.superview!.right – 5 //// curOptionLabel.bottom == curOptionLabel.superview!.bottom – 5 // curOptionLabel.top == curOptionLabel.superview!.top // curOptionLabel.left == curOptionLabel.superview!.left // curOptionLabel.center == curOptionLabel.superview!.center // curOptionLabel.right == curOptionLabel.superview!.right // curOptionLabel.bottom == curOptionLabel.superview!.bottom // } // } return cell } ============================================================================ import UIKit import Cartography //let OptionCollectionViewCellId = “OptionCollectionViewCellId” class OptionCollectionViewCell: UICollectionViewCell { let curOptionLabel:UILabel let isChoice:Bool override init(frame: CGRect){ curOptionLabel = UILabel() isChoice = Bool() super.init(frame: frame) } required init?(coder aDecoder: NSCoder) { fatalError(“init(coder:) has not been implemented”) } convenience init(curOption:String, reuseIdentifier:String?){ self.init(frame: CGRectZero) // self.reuseIdentifier = reuseIdentifier self.layer.cornerRadius = 12 self.layer.borderWidth = 1 self.layer.borderColor = UIColor.clearColor().CGColor if isChoice { self.backgroundColor = OptionCellBackgroundColorSelected self.curOptionLabel.textColor = UIColor.whiteColor() } else { self.backgroundColor = OptionCellBackgroundColorNotSelected self.curOptionLabel.textColor = CommonTextColorBlack } self.curOptionLabel.text = curOption self.curOptionLabel.font = OptionCellItemLabelFont self.curOptionLabel.textAlignment = .Center self.contentView.addSubview(self.curOptionLabel) constrain(self.curOptionLabel) {curOptionLabel in curOptionLabel.top == curOptionLabel.superview!.top curOptionLabel.left == curOptionLabel.superview!.left curOptionLabel.center == curOptionLabel.superview!.center curOptionLabel.right == curOptionLabel.superview!.right curOptionLabel.bottom == curOptionLabel.superview!.bottom } } } |
在iOS模拟器中运行出错:
2016-06-11 17:11:10.945 SalesApp[8935:243184] *** Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.60.7/UICollectionView.m:1555 2016-06-11 17:11:10.954 SalesApp[8935:243184] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘the cell returned from -collectionView:cellForItemAtIndexPath: does not have a reuseIdentifier – cells must be retrieved by calling -dequeueReusableCellWithReuseIdentifier:forIndexPath:’ *** First throw call stack: ( 0 CoreFoundation 0x000000010ece6d85 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010e75adeb objc_exception_throw + 48 2 CoreFoundation 0x000000010ece6bea +[NSException raise:format:arguments:] + 106 3 Foundation 0x000000010e3a4d5a -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198 4 UIKit 0x000000011021d249 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:] + 925 5 UIKit 0x0000000110220d96 -[UICollectionView _updateVisibleCellsNow:] + 4988 6 UIKit 0x0000000110225575 -[UICollectionView layoutSubviews] + 258 7 UIKit 0x000000010fa60980 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703 8 QuartzCore 0x000000010f85cc00 -[CALayer layoutSublayers] + 146 9 QuartzCore 0x000000010f85108e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 10 QuartzCore 0x000000010f850f0c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 11 QuartzCore 0x000000010f8453c9 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 12 QuartzCore 0x000000010f873086 _ZN2CA11Transaction6commitEv + 486 13 UIKit 0x000000010f9d219b _afterCACommitHandler + 174 14 CoreFoundation 0x000000010ec0bc37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 15 CoreFoundation 0x000000010ec0bba7 __CFRunLoopDoObservers + 391 16 CoreFoundation 0x000000010ec017fb __CFRunLoopRun + 1147 17 CoreFoundation 0x000000010ec010f8 CFRunLoopRunSpecific + 488 18 GraphicsServices 0x0000000113b6bad2 GSEventRunModal + 161 19 UIKit 0x000000010f9a5f09 UIApplicationMain + 171 20 SalesApp 0x000000010d4073f2 main + 114 21 libdyld.dylib 0x00000001115a892d start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException |
2016-06-11 16:52:49.059 SalesApp[8381:228574] *** Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.60.7/UICollectionView.m:1555 2016-06-11 16:52:49.062 SalesApp[8381:228574] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘the cell returned from -collectionView:cellForItemAtIndexPath: does not have a reuseIdentifier – cells must be retrieved by calling -dequeueReusableCellWithReuseIdentifier:forIndexPath:’ *** First throw call stack: ( 0 CoreFoundation 0x000000010f222d85 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010ec96deb objc_exception_throw + 48 2 CoreFoundation 0x000000010f222bea +[NSException raise:format:arguments:] + 106 3 Foundation 0x000000010e8e0d5a -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198 4 UIKit 0x0000000110759249 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:] + 925 5 UIKit 0x000000011075cd96 -[UICollectionView _updateVisibleCellsNow:] + 4988 6 UIKit 0x0000000110761575 -[UICollectionView layoutSubviews] + 258 7 UIKit 0x000000010ff9c980 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703 8 QuartzCore 0x000000010fd98c00 -[CALayer layoutSublayers] + 146 9 QuartzCore 0x000000010fd8d08e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 10 QuartzCore 0x000000010fd8cf0c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 11 QuartzCore 0x000000010fd813c9 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 12 QuartzCore 0x000000010fdaf086 _ZN2CA11Transaction6commitEv + 486 13 UIKit 0x000000010ff0e19b _afterCACommitHandler + 174 14 CoreFoundation 0x000000010f147c37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 15 CoreFoundation 0x000000010f147ba7 __CFRunLoopDoObservers + 391 16 CoreFoundation 0x000000010f13d7fb __CFRunLoopRun + 1147 17 CoreFoundation 0x000000010f13d0f8 CFRunLoopRunSpecific + 488 18 GraphicsServices 0x00000001140a7ad2 GSEventRunModal + 161 19 UIKit 0x000000010fee1f09 UIApplicationMain + 171 20 SalesApp 0x000000010d9420a2 main + 114 21 libdyld.dylib 0x0000000111ae492d start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException |
the cell returned from -collectionView:cellForItemAtIndexPath: does not have a reuseIdentifier – cells must be retrieved by calling -dequeueReusableCellWithReuseIdentifier:forIndexPath:
swift uicollectionviewcell reuseidentifier
ios – Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath: – Stack Overflow
iphone – UICollectionView registerCell – Blank Cells – Stack Overflow
ios – Unable to dequeue a cell with identifier – Stack Overflow
uicollectionviewcell the cell returned from -collectionView:cellForItemAtIndexPath: does not have a reuseIdentifier
注释掉:
self.optionListCollectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: OptionCollectionViewCellId) |
试试
结果问题依旧。
搜:
uicollectionviewcell Assertion failure in UICollectionView.m
iphone – iOS Assertion Failure in UICollectionView – Stack Overflow
ios – Assertion failure in UICollectionView – Stack Overflow
IOS 手写UICollectionView – B9班的真高兴 – 博客频道 – CSDN.NET
Storyboard中,UICollectionViewCell重用机制问题 | iOS开发 – CocoaChina CocoaChina_让移动开发更简单
Assertion failure in UICollectionViewCell – www.scriptscoop2.com
iOS Assertion Failure in UICollectionView (iOS) – Codedump.io
custoimize UICollectionViewCell
ios – How to create a custom UICollectionViewCell and add image from URL to it? – Stack Overflow
ios – Initializing a custom UICollectionViewCell – Stack Overflow
目前的结论是:
自定义UICollectionViewCell时,好像没法传递额外的参数
-》只有系统默认的frame参数
-》然后可以在自定义UICollectionViewCell内部,添加对应的其他自己的子view,比如此处的label,以及对应的自动约束的条件,
后来,又遇到类似问题:
代码:
class GridCell: UICollectionViewCell { var contentLabel:UILabel override init(frame: CGRect) { self.contentLabel = UILabel() super.init(frame: frame) self.contentLabel.text = “” self.contentLabel.font = GridCellContentLabelFont self.contentLabel.textColor = GridCellContentLabelTextColor self.contentView.addSubview(self.contentLabel) constrain(self.contentLabel) { contentLabel in contentLabel.edges == contentLabel.edges contentLabel.center == contentLabel.superview!.center } } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let horizontalTitleCell = GridCell() } |
但是出错:
*** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘the cell returned from -collectionView:cellForItemAtIndexPath: does not have a reuseIdentifier – cells must be retrieved by calling -dequeueReusableCellWithReuseIdentifier:forIndexPath:’ *** First throw call stack: ( 0 CoreFoundation 0x022f1494 __exceptionPreprocess + 180 1 libobjc.A.dylib 0x01b46e02 objc_exception_throw + 50 2 CoreFoundation 0x022f132a +[NSException raise:format:arguments:] + 138 3 Foundation 0x017c2322 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 118 4 UIKit 0x0392079b -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:] + 877 5 UIKit 0x039202ff -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 65 6 UIKit 0x03924761 -[UICollectionView _updateVisibleCellsNow:] + 6023 7 UIKit 0x039297df -[UICollectionView layoutSubviews] + 254 8 UIKit 0x030673d4 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 810 9 libobjc.A.dylib 0x01b5b059 -[NSObject performSelector:withObject:] + 70 10 QuartzCore 0x02e5d096 -[CALayer layoutSublayers] + 144 11 QuartzCore 0x02e508b6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 388 12 QuartzCore 0x02e5071a _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26 13 QuartzCore 0x02e42ee7 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 317 14 QuartzCore 0x02e77847 _ZN2CA11Transaction6commitEv + 561 15 QuartzCore 0x02e790b8 _ZN2CA11Transaction17flush_transactionEv + 50 16 UIKit 0x02fc8f55 _afterCACommitHandler + 197 17 CoreFoundation 0x0220375e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30 18 CoreFoundation 0x022036be __CFRunLoopDoObservers + 398 19 CoreFoundation 0x021f903c __CFRunLoopRun + 1340 20 CoreFoundation 0x021f8846 CFRunLoopRunSpecific + 470 21 CoreFoundation 0x021f865b CFRunLoopRunInMode + 123 22 GraphicsServices 0x07604664 GSEventRunModal + 192 23 GraphicsServices 0x076044a1 GSEventRun + 104 24 UIKit 0x02f96eb9 UIApplicationMain + 160 25 Sales App 0x0027b5b1 main + 145 26 libdyld.dylib 0x04af7a25 start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException |
然后改为之前的调用方式,就可以了:
开始的时候,注册自己的cell:
class GridView: UICollectionView, UICollectionViewDataSource, UICollectionViewDelegate { init(columnItemSizeList:[CGSize] = [CGSize](), contentList:[[String]] = [[String]]()) { …. super.init(frame: gridViewFrame, collectionViewLayout: gridViewLayout) self.registerClass(GridCell.self, forCellWithReuseIdentifier: GridCellId) self.delegate = self self.dataSource = self } |
然后调用的时候,必须去使用dequeueReusableCellWithReuseIdentifier:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { if indexPath.section == 0 { // let horizontalTitleCell = GridCell() let horizontalTitleCell = collectionView.dequeueReusableCellWithReuseIdentifier(GridCellId, forIndexPath: indexPath) as! GridCell } |
然后就可以正常运行并显示了自定义的UICollectionViewCell了:
转载请注明:在路上 » [基本解决]swift自定义实现UICollectionViewCell然后调用出错: the cell returned from collectionView cellForItemAtIndexPath does not have a reuseIdentifier – cells must be retrieved by calling -dequeueReusableCellWithReuseIdentifier:forIndexPath: