- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在开发一个有 UICollectionView
的应用程序。我创建了一个包含 UIImageView
和 UILabel
的自定义 Collection View Cell。以下代码导致 cellForItemAtIndexPath
方法中的 UICollectionView.dequeueReusableCellWithReuseIdentifier 出现 EXC_BAD_ACCESS (code=1, address=0x0) 错误。
这是我的代码:
import UIKit
class CategoryCell : UICollectionViewCell {
@IBOutlet var categoryImageView : UIImageView?
@IBOutlet var categoryLabel : UILabel?
}
class CatalogCollectionViewController: UIViewController, UICollectionViewDelegate , UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet var categoriesCollectionView : UICollectionView?
var screenSize : CGRect?
override func viewDidLoad() {
super.viewDidLoad()
screenSize = UIScreen.mainScreen().bounds
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: screenSize!.width/2-30 , height: screenSize!.width/2-30 )
layout.minimumInteritemSpacing = 10
layout.minimumLineSpacing = 10
self.categoriesCollectionView?.delegate = self
self.categoriesCollectionView?.dataSource = self
categoriesCollectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
self.categoriesCollectionView?.registerClass(CategoryCell.self, forCellWithReuseIdentifier: "CategoryCell")
self.categoriesCollectionView?.backgroundColor=UIColor.whiteColor()
self.view.addSubview(categoriesCollectionView!)
}
override func viewWillAppear(animated: Bool) {
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = categoriesCollectionView!.dequeueReusableCellWithReuseIdentifier("CategoryCell", forIndexPath: indexPath) as! CategoryCell
cell.layer.borderWidth = 2.0
cell.layer.borderColor = UIColor.grayColor().CGColor
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(self.view.frame.size.width/2-30, self.view.frame.size.width/2-30)
}
}
po 单元输出(调试器):
error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1,address=0x0). The process has been returned to the state before expression evaluation.
完整的堆栈跟踪:
* thread #1: tid = 0xdbea, 0x280abd6a UIKit`-[UICollectionViewData layoutAttributesForItemAtIndexPath:] + 274, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
frame #0: 0x280abd6a UIKit`-[UICollectionViewData layoutAttributesForItemAtIndexPath:] + 274
frame #1: 0x28825da4 UIKit`-[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] + 312
frame #2: 0x280abc38 UIKit`-[UICollectionView dequeueReusableCellWithReuseIdentifier:forIndexPath:] + 164
* frame #3: 0x0010d428 SwiftTest`SwiftTest.CatalogCollectionViewController.collectionView (collectionView=0x1725c200, indexPath=0x16d9c560, self=0x16dbd260)(__ObjC.UICollectionView, cellForItemAtIndexPath : __ObjC.NSIndexPath) -> __ObjC.UICollectionViewCell + 296 at CatalogCollectionViewController.swift:50
frame #4: 0x0010d850 SwiftTest`@objc SwiftTest.CatalogCollectionViewController.collectionView (SwiftTest.CatalogCollectionViewController)(__ObjC.UICollectionView, cellForItemAtIndexPath : __ObjC.NSIndexPath) -> __ObjC.UICollectionViewCell + 92 at CatalogCollectionViewController.swift:0
frame #5: 0x2881fa74 UIKit`-[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:] + 408
frame #6: 0x280abb82 UIKit`-[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 30
frame #7: 0x280a9c6e UIKit`-[UICollectionView _updateVisibleCellsNow:] + 3726
frame #8: 0x280a50aa UIKit`-[UICollectionView layoutSubviews] + 226
frame #9: 0x28041a82 UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 714
frame #10: 0x2611dad4 QuartzCore`-[CALayer layoutSublayers] + 128
frame #11: 0x261191d0 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 352
frame #12: 0x28057f9a UIKit`-[UIView(Hierarchy) layoutBelowIfNeeded] + 962
frame #13: 0x28103aac UIKit`-[UINavigationController _layoutViewController:] + 1168
frame #14: 0x2810173e UIKit`-[UINavigationController _layoutTopViewController] + 250
frame #15: 0x2811a55e UIKit`-[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:] + 634
frame #16: 0x2811a2ba UIKit`-[UINavigationTransitionView _notifyDelegateTransitionDidStopWithContext:] + 370
frame #17: 0x2811a006 UIKit`-[UINavigationTransitionView _cleanupTransition] + 758
frame #18: 0x280806b8 UIKit`-[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 204
frame #19: 0x2807e9ec UIKit`+[UIViewAnimationState popAnimationState] + 324
frame #20: 0x2810d060 UIKit`-[UINavigationTransitionView transition:fromView:toView:] + 1732
frame #21: 0x2810c996 UIKit`-[UINavigationTransitionView transition:toView:] + 26
frame #22: 0x28103278 UIKit`-[UINavigationController _startTransition:fromViewController:toViewController:] + 2548
frame #23: 0x2810251a UIKit`-[UINavigationController _startDeferredTransitionIfNeeded:] + 834
frame #24: 0x2810216c UIKit`-[UINavigationController __viewWillLayoutSubviews] + 52
frame #25: 0x281020e6 UIKit`-[UILayoutContainerView layoutSubviews] + 214
frame #26: 0x28041a82 UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 714
frame #27: 0x2611dad4 QuartzCore`-[CALayer layoutSublayers] + 128
frame #28: 0x261191d0 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 352
frame #29: 0x26119060 QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 16
frame #30: 0x26118580 QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 368
frame #31: 0x26118232 QuartzCore`CA::Transaction::commit() + 614
frame #32: 0x261119ee QuartzCore`CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 138
frame #33: 0x23e70b20 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 20
frame #34: 0x23e6ee16 CoreFoundation`__CFRunLoopDoObservers + 282
frame #35: 0x23e6f254 CoreFoundation`__CFRunLoopRun + 972
frame #36: 0x23dc1bb8 CoreFoundation`CFRunLoopRunSpecific + 516
frame #37: 0x23dc19ac CoreFoundation`CFRunLoopRunInMode + 108
frame #38: 0x2503baf8 GraphicsServices`GSEventRunModal + 160
frame #39: 0x280adfb4 UIKit`UIApplicationMain + 144
frame #40: 0x00108050 SwiftTest`main + 164 at AppDelegate.swift:12
最佳答案
这样做比较好,
if let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CategoryCell", forIndexPath: indexPath) as? CategoryCell { .....(rest of your method code) }
通过这种方式,您可以确保在尝试在其上运行代码之前确实拥有一个可重用的单元格。此外,最好使用方法 collectionView 而不是任何 socket 。
关于ios - Swift - Collection View dequeueReusableCellWithReuseIdentifier EXC 错误访问错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34516625/
我正在编写一些帐户创建代码并 try catch 特定的 sqlalchemy 异常,以便当用户使用已关联到现有帐户的电子邮件注册帐户时,我可以反馈适当的错误消息。 发生这种情况时,我原以为会得到一个
我创建了这个适用于 vector 、链表和双链表的函数。该函数获取一个值并在容器中搜索它。如果 vlaue 在容器中,则该函数会将值插入到已存在的位置旁边。因此,如果 val=2,则 {3,2,5}
我不断收到错误访问错误,我认为这与我的 UIPickerView 有关,因为这是应用程序崩溃的时候。一切正常,直到我从 UIPickerView 中做出第九个选择。每次应用程序在第 9 个选择时崩溃。
我在使用 swift 2 的最新版本的 xcode 中遇到此错误 上线 let s = linkTxt.text linkTxt 中的文本通过按钮“pasteFromClipBoard”显示 let
我有一个 Massive View Controller 并试图将我的代码分成不同的类。我创建了一个类 CurrentLocation。在 View Controller 中,我调用了 google
我正在使用 sqlacodegen 从我的数据库中反射(reflect)一堆表。我收到以下错误: sqlalchemy.exc.AmbiguousForeignKeysError: Can't det
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 8 年前。 Improve t
好吧,我对 ASP.NET 和 MasterPage 概念还很陌生,有一个我无法弄清楚的错误。 这是我的 default.aspx 的一部分: You've got credits.
本文整理了Java中com.ctc.wstx.exc.WstxException类的一些代码示例,展示了WstxException类的具体用法。这些代码示例主要来源于Github/Stackoverf
我在模拟器中遇到了问题。模拟器偶尔会停止并显示 程序收到信号:“EXC_BAD_ACCESS”。 作为控制台输出。没有提供更多信息。有机会更接近问题吗? 最佳答案 我看到 NSZombie 已经被提议
我很可能在这里缺少一些基本概念,但我尝试创建一些类似简单的代码审查应用程序的东西,并在设计数据库架构时尝试遵循此文档: http://flask-sqlalchemy.pocoo.org/2.1/mo
这种情况只是偶尔发生一次。当我从菜单项调用的方法中的断点单步执行时,按下时我最终到达方法的末尾,当我单步执行时,我最终到达 ccTouchEnded,然后发生错误访问。调试输出窗口中没有显示任何内容,
我在获取随机人变量的生日或电子邮件时遇到 EXC_BAD_ACCESS 问题。我尝试使用以下代码,但当我使用 dispatch_async 运行它时失败了。 ABAddressBook address
我收到此错误: >>> br = Browser() >>> br.open("http://www.bestforumz.com/forum/") >> >>> br.select_form(nr=
我开始使用 DeviceMotion 类进行编码。遵循 Apple 的文档后,我有以下内容: - (void)viewDidLoad { [super viewDidLoad]; my
我正在学习 Pyramid 教程:http://pyramid-blogr.readthedocs.io/en/latest/project_structure.html并使用 Windows 8。
我正在 try catch mysql/sqlalchemy OperationalErrors 并替换 handle access denied (1045) 与 connection refuse
为什么我会得到 TraceBack sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between pare
过去 3 天我一直在为这个错误而苦苦挣扎,但没有成功。我正在开发一个包含 uiviewcontrollers 和 uitableviewcontrollers 的组合应用程序,在表格 View 中我单
给定以下 python 代码: import git try: raise git.exc.GitCommandError("dummy", "foo") except git.exc.Git
我是一名优秀的程序员,十分优秀!